美文网首页iOS Developer
iOS Tableview 缓存池的发现

iOS Tableview 缓存池的发现

作者: 柯浩然 | 来源:发表于2018-03-26 21:23 被阅读815次

项目中出现了一个Bug,之后在解这个Bug 的过程中发现了一些有趣的东西,所以今天把这个记录下来。

  • 通过这个Bug,调试之后知道,tableview 的调用顺序
    1.调用 heightForRow 方法来确定 scrollview 的 contentSize,
    2.调用 cellForRow 方法来确定 cell 的内容是什么
    3.再次调用 heightForRow 方法确定最终的 cell 显示的高度和内容。

也就是说在第三次调用之前,我们写的 cell 的高度只是一个默认值,这时的 height 值并不能作为一个参考值使用

  • 项目中的 bug 情况是由以下代码引起的
[self.tableview reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];

过程就不多说,直接说结果
以上方法使用的缓存池和以下方法使用的缓存池不是同一个

[self.tableview reloadData]

代码逻辑是这样的,先有一个数据源,改变数据源之后只更新当前section

self.dataArray = @[@[@"中国", @"韩国"],@[ @"美国", @"德国"]].mutableCopy;
[self.tableview reloadData];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

     self.dataArray[0] = @[@"橘子",@"柚子"];
     [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
});

验证如下

未调用 reloadSections 的时候打印 self.tableview.subviews 是这样的 第一次打印 调用 reloadSections 的之后打印 self.tableview.subviews 是这样的 reloadSections调用之后

可以看到调用之后 tableview 的缓存池里面的 cell 多了两个。

  • 改变代码,验证一下
self.dataArray = @[@[@"中国", @"韩国"],@[ @"美国", @"德国"]].mutableCopy;
[self.tableview reloadData];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

     self.dataArray[0] = @[@"橘子",@"柚子"];
    [self.tableview reloadData];
});
上面代码之后的打印结果如下图:

要是这么写代码

    self.dataArray = @[@[@"中国", @"韩国"],@[ @"美国", @"德国"]].mutableCopy;
    [self.tableview reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.dataArray[1] = @[@"橙子",@"栗子"];
        [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
        self.dataArray[0] = @[@"橘子",@"柚子"];
        [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
    });
打印的结果就是这样了

我猜想 reloadSections 方法是异步进行的
验证如下

   self.dataArray = @[@[@"中国", @"韩国"],@[ @"美国", @"德国"]].mutableCopy;
    [self.tableview reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.dataArray[1] = @[@"橙子",@"栗子"];
        [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
    });
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
     self.dataArray[0] = @[@"橘子",@"柚子"];
    [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
    });
   
    
结果如下图

可以验证猜想正确

要是这么写

    self.dataArray = @[@[@"中国", @"韩国"],@[ @"美国", @"德国"]].mutableCopy;
    [self.tableview reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        self.dataArray[0] = @[@"橘子",@"柚子"];
        [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
        self.dataArray[1] = @[@"橙子",@"栗子"];
        [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        self.dataArray = @[@[@"iPhone", @"iPod"],@[ @"iMac", @"iPad"]].mutableCopy;
        [self.tableview reloadData];
    });
debug 图片可以看出来reloadData 方法是首先复用 reloadSections 的缓存 cell的

最后想说的是,出屏 cell 的缓存池复用是不存在这种差异化的。

3 月 27 日更新:
经过查看 reloadSections 的官方文档对此方法的解释,因为没有 OC 的源码,只能对上面的现象做一些合理的猜测。

  • reloadSections 方法是一定会 new cell 的
  • dequeue 方法会优先拿屏幕上显示的 cell 来复用。
  • 使用 reloadSections 方法在 new 完之后把原先屏幕上的 cell 挤入了缓存池,这些 cell 在 tableview 未滑动出屏之前都不会被使用

相关文章

网友评论

    本文标题:iOS Tableview 缓存池的发现

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