UITableViewWrapperView:tableViewGroup类型出现空白

时间:2019年1月25日 周五

问题:使用tableViewGroup类型,这种类型方便我们分栏,滚动时section的view会跟着一起动;
但是出现了一个异常的情况,tableView会出现空白的区域,而且是不确定性的空白,点击视图可以看到是UITableViewWrapperView出了问题,百度一下解决方法,但是都不是很好的解决策略,还有很多方法试了也行不通。
比如:

self.edgesForExtendedLayout = UIRectEdgeNone;
self.automaticallyAdjustsScrollViewInsets = NO;

经多次的调试分析,这个应该是tableView的一个隐形的bug。出现在tableView的属性设置值,但是又没有实现的情况下导致,可以推断tableView在创建的时候会取相应的值,有值但提供view设置不对,就会使用默认值。
1)简单的解决方法:就是不使用这些属性,设置高度就行了,如果有自定义的view话,要使用重用机制。
直接创建返回,这样是不行的:

return [[UIView new] initWithFrame:(CGRect){0,0,CGRectGetWidth([UIScreen mainScreen].bounds),30}];

2)如果直接给tableView属性设置值的时候,可能出现空白的情况,以及解决方法如下。
注意:frame非zero,要使用重用机制

1、tableheaderview
tableView.tableHeaderView = [UIView new];//会导致头部出现空白
解决:要给headerview提供一个frame非zero的view

2、tablefooterview
tableView.tableFooterView = [UIView new];//底部不会出现空白

3、cell
tableView.estimatedRowHeight = 42;//因为实现了cell,所以不会出现空白
tableView.rowHeight = 42;//因为实现了cell,所以不会出现空白

4、section headerview
tableView.estimatedSectionHeaderHeight = 30;//会导致每行section出现空白
解决方法:
1)要实现sectionheader的方法,返回headerview,且frame不能为zero
方法: - (UIView *)tableView: viewForHeaderInSection:
2)view要实现复用,不然还会出现空白

[tableView dequeueReusableHeaderFooterViewWithIdentifier: @"header"];
如:
- (CGFloat)tableView:(UITableView *)tableView  heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *headerView = (UILabel *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:TableViewHeaderViewIdentifier];
    if (!headerView) {
        headerView = [UILabel new];
        headerView.backgroundColor = [UIColor whiteColor];
        headerView.frame = (CGRect){0,0, CGRectGetWidth([UIScreen mainScreen].bounds),30};
    }
    headerView.text = [NSString stringWithFormat:@"  %@",self.indexArray[section]];
    return headerView;
}

5、section footer view
tableView.estimatedSectionFooterHeight = 30;//会导致每行section出现空白
解决方法:
1)要实现sectionfooter的方法,返回footerview,且frame不能为zero
方法: - (UIView *)tableView: viewForFooterInSection:
2)view要实现复用,不然还会出现空白

[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"footer"]
如:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *headerView = (UILabel *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"footer"];
    if (!headerView) {
        headerView = [UIView new];
        headerView.backgroundColor = [UIColor whiteColor];
        headerView.frame = (CGRect){0,0, CGRectGetWidth([UIScreen mainScreen].bounds),30};
    }
    return headerView;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容