您的位置:首页 > 其它

有关分组TableView的组高设置

2016-04-12 14:34 309 查看
iOS的tableView的模式有2种,一种是UITableViewStylePlain不分组的样式,UITableViewStyleGrouped分组样式。

1、对于不分组的样式,通常实现几个代理方法就能够完成效果,但是有一点就是tableView的headerView不会跟着tableView一起滚动(滚动的时候,会留在页面上面,等待下一个footerView顶上来的时候才会消失);

2、如果要实现footerView随时跟着tableView滚动,就需要设定UITableViewStyleGrouped分组样式。由于tableView的style是readonly,所以只能在创建表的时候设置

-(instancetype)init{
if (self = [super initWithStyle:UITableViewStyleGrouped]) {
}
return self;
}


如果你用的是xib,在属性栏那里同样可是设置

3、设置成功之后发现运行效果确实是分组模式,footerView也可以跟着滚动了,但是section的高度和预期的不一样,这就需要设置sectionHeaderHeight,sectionFooterHeight的高度了,在没有设置之前,它们的高度都是-1.000000,是系统的默认值。

4、在没有设置sectionHeaderHeight,sectionFooterHeight之前,很多同学刚开始可能会选择实现代理方法

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


但会发现组高的确实发生了变化,但是所有的组高并不相等,然后实现方法

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


会发现仍没有太大变化,就不知所措了,没关系,不用上面的方法,我们可以试一下下面方法:

self.tableView.sectionHeaderHeight = 20;
self.tableView.sectionFooterHeight = 0;


这时你会发现section=0 的时候并没有变化,所以我们还需要

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


到此为止,自定义高度就完成了,并且想要改变组高,只需要修改sectionHeaderHeight或者sectionFooterHeight就行了,当然也可以在代理方法中判断section来修改。

5、总结一下:最后发现self.tableView.sectionHeaderHeight = 20;可以不用设置,直接用代理的方法就可以,但是self.tableView.sectionFooterHeight = 0必须写。

并且如果写了self.tableView.sectionFooterHeight = 0之后,他的代理方法其实是不起作用的。简单来说,就是只需实现下面的代码:

self.tableView.sectionFooterHeight = 0;

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}


就完成了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: