确定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