UITableViewStyleGrouped类型的UITabe

作者: Code_Ninja | 来源:发表于2015-11-22 18:13 被阅读18147次

我们知道使用UITableView的时候有个技巧:使用table.tableFooterView = [UIView new];一行代码可以解决UITableView在cell比较少的情况下不显示下面的分割线条How to remove empty cells in UITableView?
今天在使用UITableViewStyleGrouped类型的UITableView的时候又发现一个小技巧。
当设置UITableView为UITableViewStyleGrouped的时候,下面两段代码将导致不同的界面效果:

方式1:

  table = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];

  table.backgroundColor = [UIColor  clearColor];

  table.separatorColor = kLineColor;

  table.tableFooterView = [UIView new];

  table.delegate = self;

  table.dataSource = self;

方式1的效果图:

方式1效果图.png

方式2:

table = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];

table.backgroundColor = [UIColor  clearColor];

table.separatorColor = kLineColor;

table.delegate = self;

table.dataSource = self;

table.tableFooterView = [UIView new];

方式2的效果图:

方式2的效果图.png

前提条件:

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
     return 10.f;
}

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section

{
      return 0.01f;
}

我们看到代码顺序不同直接导致了界面显示不同。我们看到第一种情况,这什么鬼?

问题1:如何调整第一个section header 的默认高度

我相信肯定有不少人遇到过这种情况---怎么修改UITableViewStyleGrouped类型的UITableView的第一个sction header的默认高度?,然后网上各种搜怎么解决UITableViewStyleGrouped类型的UITableView的第一个section header的高度问题,然后你会搜到这种解决方案:

table.contentInset = UIEdgeInsetsMake(-25, 0, 0, 0);通过这行代码来将UITableView的第一个section header隐藏一部分上去。

通调整可以看到下面的效果,会比之前好多了,但是在实际使用过程中要达到精确的section header 高度,需要多次调整contentInset来看效果,颇为麻烦。

效果图.png

问题2:如何改变默认section footer 默认高度

在stackoverflow上面会有人告诉你,可以通过以下设置比0稍微大一点点的方式来改变section footer的高度。

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
     return 0.01;
}

见:How to change height of grouped UITableView header?
How to hide first section header in UITableView (grouped style)

总结:

在使用UITableViewStyleGrouped类型UITableView的时候,要想去掉头部默认高度,建议使用以下代码:

table = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
table.backgroundColor = [UIColor clearColor];
table.separatorColor = kLineColor;
table.delegate = self;
table.dataSource = self;
table.tableFooterView = [UIView new];

//同理,tableHeaderView也应该在设置代理之后赋值

以及:

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
     return 10.f;
}

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
     return 0.01f;
}

具体为什么table.tableFooterView = [UIView new];与
table.delegate = self;
table.dataSource = self;
顺序颠倒会导致UITableViewStyleGrouped类型UITableView的效果造成不一样的影响,对UITableViewStylePlain类型的UITableView却没有什么影响,看苹果官方文档中,对tableFooterView也没有做很详细的说明,我猜测是UITableViewStyleGrouped类型UITableView在runtime中做了特殊处理。

相比之下,第二种代码实现的方式更简单明了,只需要注意代码顺序即可。在前期没发现第二种实现方法,导致一直按照第一种方式折腾了好久,走了不少弯路,特做此记录,转载请注明出处。


2016-09-06更新

感谢 HYY在第七楼分享的更加直接粗暴的方法:

table.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)]; 

来实现去掉UITableViewStyleGrouped类型UITableView头部高度,但是为了调整分区之间的间距还是需要实现heightForFooterInSection方法的。

相关文章

网友评论

  • Coder_Cat:直接设置 _tableView.sectionHeaderHeight = 0; _tableView.sectionFooterHeight = 0;即可,设置属性好用,设置代理方法不好用,亲测有效!
    Code_Ninja:不错,如果每个分区间距是一样的话,直接赋值会更好。不过我个人更倾向于实现代理方法,因为使用代理方法可以更灵活地调整每个分区的间距(比如考虑到以后扩展)。
  • WolfTin:我现在有tableHeaderView,组头,tableFooterView,当没有cell的时候,tableFooterView和组头中间有一个间隔,我不希望有这个间隔,尴尬啊😅
    Code_Ninja:这个间距其实是sectionfooter,加上以下代码即可消除。
    ```
    -(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
    {
    return 0.01f;
    }
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    {
    return nil;
    }
    ```
    恩,所以还是尽量使用UITableViewStylePlain吧,UITableViewStylePlain类型的完全可以实现UITableViewStyleGrouped类型的效果,而且不会有这么多奇怪的问题。
  • 易明轩轩主:_settingTableVIew.sectionFooterHeight = 10;
    _settingTableVIew.sectionHeaderHeight = 0.01;
    _settingTableVIew.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, CGFLOAT_MIN)];
    这么写, 连代理方法都省掉了
    Code_Ninja:是的,在每个sectionFooterHeight和sectionHeaderHeight固定的情况下这样写没毛病。如果高度不固定,还是得写代理方法。这里主要是想说明一下,以下三行代码顺序不同导致不同效果:
    table.delegate = self;
    table.dataSource = self;
    table.tableFooterView = [UIView new];
  • 吃蘑菇De大灰狼:UITableViewStyleGrouped的情况,需要实现Header和Footer高度的代理方法,返回值要大于0:
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
    }
  • 洁简:每个section都有横线怎么去掉呢?
    Code_Ninja:@洁简 实在不行就用Plain类型的UITableView,自定义sectionheader sectionfooter一样的效果。
    洁简: @Code_Ninja 这样都会改变。我想只改两头的
    Code_Ninja:table.separatorColor = table.backgroundColor;
  • 37c7796cd658:果然都是大神 ,完美解决了我的问题
  • f9a946b41c32:困扰了很久的问题.. 感谢博主分享
  • 回希:正郁闷呢,结果就遇到你了。哈哈
  • seasonZhu:这个确实困扰了我很久啊,没想到用这种方式给解决了,一般确实用Plain情况更多一些。
  • wMellon:self.automaticallyAdjustsScrollViewInsets = NO;加上这句搞定
  • 草原野马:我这边遇到一个这样的问题,现在我必须用UITableViewStyleGrouped这种格式的,但是我还要给tableView加一个head,所以加出来的head,高度总是会多一个35,这种情况有没有办法。
    草原野马:@Code_Ninja ok
    Code_Ninja:设置tableHeaderView不会出现你这种情况啊,创建的时候就给它固定高度。
  • 遛遛食:UITableViewController的tableview怎么设置成group样式啊?
    遛遛食:@Code_Ninja 我是重写的这个方法
    遛遛食:@Code_Ninja 试了不管用,程序会崩溃
    Code_Ninja:@遛遛食 它有一个- (instancetype)initWithStyle:(UITableViewStyle)style方法。
  • 日拱一卒wu:你好,为什么看到你的每个group都有上下划线边框,但是我的没有?只有cell有下划线。
    日拱一卒wu:@Code_Ninja 应该是背景色的问题,我的背景色比较浅,没有你的明显,你的看着还以为是下划线边框,谢谢
    Code_Ninja:@Wxwldu 额,我的也是只有cell有下划线的。你说的效果可能是我的vc.view设置了背景色
  • 小多多:额,那个,0.01f为啥加一个f,虽然一直这样用,但是不知道原理,大神知道吗
    Code_Ninja:@小多多 表示是float类型的数据,可以省去数据类型转换,不过这里可以不加
  • 李乾坤David:苹果真的很贱啊!来个0.01就行了!

  • 李乾坤David:太棒了!
    hypercode:在一起
    李乾坤David:@李乾坤David 可以的!
    坤哥lqk:你好,咱们两个重名重姓,还都做iOS开发,还遇到同样的问题,好巧啊
  • HYY:table.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)]; OK
    Code_Ninja:@HYY :+1: 此方法甚好!感谢分享!
  • feng_dev:写的很详细啊 大神
  • KevinMK:哈哈,支持 奇淫巧计~~褒义的~~~
  • ece172c77b92:我有个问题,grouped这个类型tableview很奇怪,tableview吊用relodsectionatIndexPath方法时候如果这个saction有header就会刷完异常了,怎么解决?我觉得也是和runtime有关,reloaddata这个方法就没事了
    超_iOS:@Code_Ninja 链接文中每个Section中的row是动态变化的,到底是怎么变化的能讲下吗
    ece172c77b92:@Code_Ninja 谢谢
    Code_Ninja:@ece172c77b92 是的,这个问题不光是grouped类型的UITableView有这个问题,单独刷新一个section的时机不对,或者同一时间刷新不同的section都有可能导致数组越界。可以看下这个文章:http://blog.csdn.net/iosswift/article/details/50001145
  • 程序媛coco:代码有好几处都没加空格 看的怪怪的 麻烦校正下格式~
    Code_Ninja:@CoderLotus 哈哈,是复制过来的时候出现的小细节问题,就几行代码而已,请不要在意这些细节。
  • 纪叙:这个发现有点意思,虽然我平时不喜欢这个类型的table

本文标题:UITableViewStyleGrouped类型的UITabe

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