美文网首页程序员将来跳槽用恩美第二个APP项目
iOS 11 适配(一)UITableViewStyleGrou

iOS 11 适配(一)UITableViewStyleGrou

作者: 你看见我的小象了吗 | 来源:发表于2017-10-12 20:35 被阅读606次

适配 iOS 11 是最近工作中的重中之重,发现的问题也有很多。

UITableViewStyleGrouped 类型的 tableView 在适配的时候出现很大的问题。记录一下

按照之前的方法,只需要执行以下的代码就能够很好的解决 section == 0 的时候,sectionHeader 的高度问题以及 section 间距的问题

tableView.delegate = self;
tableView.dataSource = self;
tableView.tableFooterView = [UIView new];

配合这两种方法

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

但是,在 iOS 11 上通过这两种方法已经不能解决问题,通过大量的测试,始终发现,在 iOS 10 以下版本中都能够解决问题,但是在 iOS11 上不能够解决问题,经过研究之后发现,通过以下的方法能够良好的解决 sectionHeader 的高度问题,并且是兼容 iOS 10 以及其他版本的

tableView.delegate = self;
tableView.dataSource = self;
tableView.sectionFooterHeight = 0.01f;
tableView.tableFooterView = [UIView new];

  1. 首先,在实例化tableView 的时候,直接声明 sectionFooterHeight,不需要写代理方法
  2. 声明 sectionHeaderView 高度的时候,不能够再像以前一样仅仅声明高度,而是,直接使用比较粗暴的方式,声明一个 view 出来 ,这样就能像从前一样实现效果

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ViewWidth, 10.0f)];
return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10.0f;
}

两种方法必须同时写,不然还是不生效,不知道因为什么,从苹果的官方文档还是没有获取到关于footerView 的更多解释,猜测可能还是在 runtime 的时候,苹果做了不知道的事情

亲测是这样的效果,如果大神能够有更好的解决方式,希望提供,欢迎拍砖

更新:
昨天晚上比较匆忙,忘记测试了两项比较重要的属性,今天更新一下,发现只要设置以下属性,能够良好的解决问题

tableView.estimatedSectionHeaderHeight = 0;
tableView.estimatedSectionFooterHeight = 0;

设置 tableView 的sectionHeader、FooterView 的高度之后,就就可以解决高度不准确的问题
在系统更新之后,tableView 的 section 的头尾预估高度不再是0,官方文档解释如下

// default is UITableViewAutomaticDimension, set to 0 to disable

感谢各位大神

相关文章

网友评论

    本文标题:iOS 11 适配(一)UITableViewStyleGrou

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