UITableViewStyleGrouped类型的UITabelView使用技巧

转发自:作者 Code_Ninja

我们知道使用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];

以及:

-(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方法的。

 



作者:Code_Ninja
链接:http://www.jianshu.com/p/6597b51e6098
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

posted @ 2017-07-20 23:07  星星太阳鱼虾蟹  阅读(320)  评论(0编辑  收藏  举报