確定section與row代碼:
//幾個section
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
return 6;
}
//每個section包含的row
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
switch(section) {
case0:
return 1;
break;
case1:
return 1;
break;
case2:
return 2;
break;
case3:
return 1;
break;
case4:
return 1;
break;
case5:
return 1;
break;
default:
return 0;
break;
}
}
之后在數據源方法中確認每個section下cell的identifier
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
switch(indexPath.section) {
case0:
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
return cell;
break;
}
...
return nil;
}
下面確認每個section的title卿操,高度與row的高度
//title
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
NSString* sectionName;
switch(section){
case 0:
sectionName = self.titleArray[0];
break;
case 1:
sectionName = self.titleArray[1];
break;
case 2:
sectionName = self.titleArray[2];
break;
default:
break;
}
return sectionName;
}
//section高度
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {
switch(section) {
case 0:
return 50;
break;
case 1:
return 50;
break;
case 2:
return 50;
break;
default:
return 0;
break;
}
}
//row高度
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section {
return 40;
}
自定義section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 40)];//
//add your code behind
return view;
}
header通過下面兩個代理方法設置
(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
footer通過下面兩個
(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
如果要做整個tableview的header和footer身隐,要通過tableview setHeaderView setFooterView