美文网首页
iOS11中tableView的section设置

iOS11中tableView的section设置

作者: MichealXXX | 来源:发表于2018-08-24 17:00 被阅读0次
tableView大家再熟悉不过,偶尔我们需要cell之间有间隔,实现的方式也很多,一般我会采用UITableViewStyleGrouped,然后设置其Header,或者Footer来实现,不过昨天我开发的时候却发现了个问题,cell之间产生间隔我只需要单独设置每个cell的Header高度,并且设置Header的颜色用于分隔cell即可,于是我写下了以下代码:
    //section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 26;//section头部高度
}
//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    return view ;
}
然而我发现,每个Header距离上一个cell还是有一段距离,我想应该是UITableViewStyleGrouped选择下系统默认了cell的Footer高度,于是乎我又加了一段代码让Footer的高度为0.01,代码如下:
    //section尾部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
奇怪的是之前都是使用这种方式实现但是在iOS11上面却出现了不同的效果,每个cell下面有了一个高度为0.01的Footer(肉眼看不出,使用Xcode的视图工具可以发现)而下一个cell的Header距离上一个cell还是有一段距离,查了一下新特性之后我才发现,必须要同时设置Footer的高度以及viewForFooterInSection才能消除系统默认设置的间隔,代码如下:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
这两个方法同时设置才可以消除Cell的间隔,实现我们想要的效果

相关文章

网友评论

      本文标题:iOS11中tableView的section设置

      本文链接:https://www.haomeiwen.com/subject/bfhiiftx.html