12-TableView的头部和尾部的高度
2018-11-05 本文已影响14人
小胖子2号
一、统一设置tableview的头部和尾部的高度

// 设置头部间距
self.tableView.sectionHeaderHeight = 0;
self.tableView.sectionFooterHeight = ZSCommonMargin;
问题:设置完成后,头行上面的间距比较大

解决方法如下:
- 先调用这个方法,计算cell的frame
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
ZSLog(@"%@",NSStringFromCGRect(cell.frame));
}

2.由此可知y值为35。通过contentInset(内边距)进行设置。
// 设置内边距(-25代表:所有内容往上移动25)
self.tableView.contentInset = UIEdgeInsetsMake(ZSCommonMargin - 35, 0, 0, 0);
最终效果图:

二、分别设置不同的头部和尾部的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0) return 10;
if (section == 1) return 20;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == 0) return 10;
if (section == 1) return 20;
}
*设置头部、尾部的内容

其它正在整理中