UITableVIew 关于复用的坑

记录一个小坑
section header view的复用过程中,配置较低的手机(也可能是系统版本问题,具体没有详细验证),在tableView: viewForHeaderInSection:方法中,设置[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"xxx"],进行section header 的复用时,tableview加载的header,会提前复用
所以这里需要注意,自定义的section header view,重新赋值时一定要清除原有数据,清除原有数据包括UI组件自身,所以,如果加载的header view 内部布局不相同的情况下, 要给每一个 subview 设置为 nil或者在header view中,subview不要使用懒加载进行初始化。类似下面这种方式

// 需要复用的view,尽量少用下面这种懒加载方式初始化
- (UIImageView *)headImgV {
    if (!_headImgV) {
        _headImgV = [[UIImageView alloc] init];
    }
    return _headImgV;
}

下面是这次header复用的一些log日志,可以明显看到,当section 0 第二次加载时,直接复用了 section1第一次的header。

第一次加载

[15:18:47] -[RiskQuestionVC2021 tableView:viewForHeaderInSection:] [第352行] 需要加载sectionHeader,section:0

[15:18:47] -[RiskQuestionVC2021 tableView:viewForHeaderInSection:] [第358行] section:0 header:<Risk2021SectionHeader: 0x1025efaa0; baseClass = UITableViewHeaderFooterView; frame = (0 0; 414 211); text = ''; hidden = YES; autoresize = W; layer = <CALayer: 0x1c0635ea0>>

[15:18:47] -[RiskQuestionVC2021 tableView:viewForHeaderInSection:] [第352行] 需要加载sectionHeader,section:1

[15:18:47] -[RiskQuestionVC2021 tableView:viewForHeaderInSection:] [第358行] section:1 header:<Risk2021SectionHeader: 0x1026e49a0; baseClass = UITableViewHeaderFooterView; frame = (0 0; 0 0); text = ''; layer = <CALayer: 0x1c462f000>>

[tableview raloadData],第二次加载(增加了一个section)

[15:18:47] -[RiskQuestionVC2021 tableView:didSelectRowAtIndexPath:] [第408行] 加载新数据前,tableview高度 623.500000

[15:18:47] -[RiskQuestionVC2021 tableView:viewForHeaderInSection:] [第352行] 需要加载sectionHeader,section:0

[15:18:47] -[RiskQuestionVC2021 tableView:viewForHeaderInSection:] [第358行] section:0 header:<Risk2021SectionHeader: 0x1026e49a0; baseClass = UITableViewHeaderFooterView; frame = (0 229; 414 81.5); text = ''; hidden = YES; autoresize = W; layer = <CALayer: 0x1c462f000>>

[15:18:47] -[RiskQuestionVC2021 tableView:viewForHeaderInSection:] [第352行] 需要加载sectionHeader,section:1

[15:18:47] -[RiskQuestionVC2021 tableView:viewForHeaderInSection:] [第358行] section:1 header:<Risk2021SectionHeader: 0x1025efaa0; baseClass = UITableViewHeaderFooterView; frame = (0 0; 414 211); text = ''; hidden = YES; autoresize = W; layer = <CALayer: 0x1c0635ea0>>

[15:18:47] -[RiskQuestionVC2021 tableView:viewForHeaderInSection:] [第352行] 需要加载sectionHeader,section:2

[15:18:47] -[RiskQuestionVC2021 tableView:viewForHeaderInSection:] [第358行] section:2 header:<Risk2021SectionHeader: 0x1026e9b80; baseClass = UITableViewHeaderFooterView; frame = (0 0; 0 0); text = ''; layer = <CALayer: 0x1c46347a0>>

[15:18:47] -[RiskQuestionVC2021 loadMoreQuestion]_block_invoke [第210行] 等待0.1秒后,tableview高度 833.000000

上面log日志可以明显的看出,第一次加载时 section:1 header:<Risk2021SectionHeader: 0x1026e49a0; 第二次加载 section:0 header:<Risk2021SectionHeader: 0x1026e49a0; 在增加了一个新的section,reloaddata后,第二次的section0直接使用了第一次加载的section1。如果此时不将header view里面的subview设置为nil,或者重新alloc、init,那么header view就会直接使用第一次的UI布局,导致布局错误。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、 关于sectionHeader, Footer tableView 是plain时。 section的tit...
    落夏简叶阅读 573评论 0 0
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,174评论 1 32
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 126,651评论 2 7
  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 6,147评论 0 4