標籤:style color width 資料 string set
由於項目上的需求,需要做一個表格出來,來顯示流程狀態。剛開始腦子一頭霧水,沒有一點思路,但是靠著自己的座右銘--“世上無難事,只怕有心人”,克服了所有困難。好,不說了,講正事。
製作表格,還是需要tableView來做。
1. 建立一個UIView對象 ;
UIView *tableViewHeadView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kCount*kWidth, kHeight)];
self.myHeadView=tableViewHeadView; //(myHeadView 是 UIView)
2.建立N個欄位的View的對象,放到 上面建立的tableViewHeadView上;
for(int i=0;i<kCount;i++){
UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(i*kWidth, 0, kWidth, kHeight)];
headView.backgroundColor=[UIColorcolorWithRed:arc4random_uniform(255)/255.0green:arc4random_uniform(255)/255.0blue:arc4random_uniform(255)/255.0alpha:1];
[tableViewHeadView addSubview:headView];
}
3.然後建立一個UITableView對象 ;
UITableView *tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0, 0, self.myHeadView.frame.size.width, 460) style:UITableViewStylePlain];
tableView.delegate=self;
tableView.dataSource=self;
tableView.bounces=YES;
tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
self.myTableView=tableView;
tableView.backgroundColor=[UIColorwhiteColor];
4.建立一個UIScrollView對象,放置上面的tableView對象,並且將其位置右移部分像素;
UIScrollView *myScrollView=[[UIScrollViewalloc]initWithFrame:CGRectMake(kWidth*0.7, 0, self.view.frame.size.width-kWidth*0.7, 480)];
[myScrollView addSubview:tableView];
myScrollView.contentSize=CGSizeMake(self.myHeadView.frame.size.width,0);
myScrollView.bounces=YES;
[self.view addSubview:myScrollView];
5.建立表最左邊的的欄位的列;//有人會問,為什麼不在上面的tableview裡面一起表現出來呢。這裡,我想告訴你的是,為了實現,表格第一列不動,其他列可以滑動的效果,所以這樣做了。這樣做 還有最重要一步,就是要實現滾動非第一列的時候,保證整個tableView和 第一列同時滑動,這就是我第6步要實現的了;
self.timeView=[[TimeView alloc]initWithFrame:CGRectMake(0, 100, kWidth*0.7, kCount*(kHeight+kHeightMargin))]; //在TimeView 類裡面,建立了一個tableView
[self.view addSubview:self.timeView];
6. 實現UIScrollView的delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat offsetY= self.myTableView.contentOffset.y;
CGPoint timeOffsetY=self.timeView.timeTableView.contentOffset;
timeOffsetY.y=offsetY;
self.timeView.timeTableView.contentOffset=timeOffsetY;
if(offsetY==0){
self.timeView.timeTableView.contentOffset=CGPointZero;
}
}
7.接下來就是實現tableView的delegate 和 dataSource。
在這裡要說明一下,Mycell這個類的初始化方法裡面,又建立了N個view
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
for(int i=0;i<20;i++){
UIView *headView=[[UIViewalloc]initWithFrame:CGRectMake(i*kWidth, 0, kWidth-kWidthMargin, kHeight+kHeightMargin)];
headView.backgroundColor=[UIColor whiteColor];
[self.contentView addSubview:headView];
}
}
returnself;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return kCount-1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"cell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil){
cell=[[MyCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];
cell.backgroundColor=[UIColorgrayColor];
// cell.selectionStyle=UITableViewCellSelectionStyleNone;
[cell setSelectionStyle:UITableViewCellSelectionStyleDefault];
}
return cell;
}
8.最後非同步需要將前面第1步,建立的那個headerView,放置到表頭,並設定表頭高度;
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
returnself.myHeadView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
returnkHeight;
}
這裡只是寫了個極具簡單的表格,沒有將資料填充進去,後續會加進去的。。。