区块链技术博客
www.b2bchain.cn

关于TableView刷新的那些事求职学习资料

本文介绍了关于TableView刷新的那些事求职学习资料,有助于帮助完成毕业设计以及求职,是一篇很好的资料。

对技术面试,学习经验等有一些体会,在此分享。

前言

之前的需求一直专注于有关Feed的底层逻辑,对TableView/CollectionView的UI层的系统接口还是比较生疏,尤其是刷新部分

reloadData

reloadData是最常用的方法,调用reloadData后会调用一系列方法(顺序并不是下面所列的顺序,下面所列的方法也不全

  • numberOfSectionsInTableView
  • (NSInteger)numberOfRowsInSection:(NSInteger)section;
  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
  • (void)tableView:(UITableView )tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath)indexPath API_AVAILABLE(ios(6.0));

第一个坑就是reloadData是一个异步方法(主线程异步),也就是说执行了[tableView reloadData]会马上执行下面的代码,但此时列表并没有刷新完;

reloadData也不是开始调用就Async了,其上面所列的方法有同步和异步之分:

同步方法;numberOfSectionsInCollectionView 和 numberOfItemsInSection
异步方法:cellForItemAtIndexPath 和 heightForRowAtIndexPath

要想确保reloadData把列表刷新好了再跑下面的代码,有两种解决方法

  1. 调用layoutIfNeeded强绘
  2. 调用async-main,把后面代码也抛到异步

下面给大🔥整个Demo瞧瞧:

[tableView reloadData]; NSLog(@"data has been reload");

通过打日志输出如下:

2020-09-12 16:19:52.633305+0800 FeedDemo[56142:10761058] numberOfSectionsInTableView 2020-09-12 16:19:52.633523+0800 FeedDemo[56142:10761058] numberOfRowsInSection 2020-09-12 16:19:52.633709+0800 FeedDemo[56142:10761058] data has been reload  # 注意这里 2020-09-12 16:19:52.633891+0800 FeedDemo[56142:10761058] cellForRowAtIndexPath 2020-09-12 16:19:52.634045+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.634294+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.634488+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.634682+0800 FeedDemo[56142:10761058] cellForRowAtIndexPath 2020-09-12 16:19:52.634799+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.634971+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.635164+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.635336+0800 FeedDemo[56142:10761058] cellForRowAtIndexPath 2020-09-12 16:19:52.635530+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.635693+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.636064+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath

上面的日志正好说明了刚刚提到的两点:

  1. numberOfSectionsInTableView & numberOfRowsInSection 是同步调用,而heightForRowAtIndexPath & cellForRowAtIndexPath是异步调用
  2. 紧接着reloadData的代码会在列表没刷新完就马上调用

下面使用【强绘】的方式解决问题:

[tableView reloadData]; [tableView layoutIfNeeded]; NSLog(@"data has been reload");

日志如下:

2020-09-12 16:25:48.583714+0800 FeedDemo[56471:10766765] numberOfSectionsInTableView 2020-09-12 16:25:48.583883+0800 FeedDemo[56471:10766765] numberOfRowsInSection 2020-09-12 16:25:48.584084+0800 FeedDemo[56471:10766765] cellForRowAtIndexPath 2020-09-12 16:25:48.584216+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.584394+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.584563+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.584773+0800 FeedDemo[56471:10766765] cellForRowAtIndexPath 2020-09-12 16:25:48.584959+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.585113+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.585373+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.585598+0800 FeedDemo[56471:10766765] cellForRowAtIndexPath 2020-09-12 16:25:48.585710+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.585896+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.586069+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.586428+0800 FeedDemo[56471:10766765] data has been reload # 跑到最后了

再使用async-main的方式解决问题:

[tableView reloadData];     dispatch_async(dispatch_get_main_queue(), ^{         NSLog(@"data has been reload");     });

日志输出:

2020-09-12 16:28:49.432725+0800 FeedDemo[58104:10774035] numberOfSectionsInTableView 2020-09-12 16:28:49.432884+0800 FeedDemo[58104:10774035] numberOfRowsInSection 2020-09-12 16:28:49.433105+0800 FeedDemo[58104:10774035] cellForRowAtIndexPath 2020-09-12 16:28:49.433254+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.433512+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.433694+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.433878+0800 FeedDemo[58104:10774035] cellForRowAtIndexPath 2020-09-12 16:28:49.434090+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.434233+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.434467+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.434686+0800 FeedDemo[58104:10774035] cellForRowAtIndexPath 2020-09-12 16:28:49.434817+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.434973+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.435136+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.435930+0800 FeedDemo[58104:10774035] data has been reload # 也是在最后

Updates

下面根据这篇参考博客做一下回顾和总结

我以Updates为标题主要包括了以下内容:

- (void)beginUpdates  - (void)endUpdates - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;  - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation; - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation; - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

iOS11 又提供了begin/endUpdates的替代版performBatchUpdates

首先区分一点”数据源更改“和”UI更改“是两部操作

比如删除一行:

[self.tempArray[0] removeObjectAtIndex:1]; [self.tableView deleteRowsAtIndexPaths: indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];

都是先把数据源更改后再进行UI更改

begin/endUpdates主要是为了同时进行多个增删改(reload)操作,但要注意的是:

  1. begin/endUpdates内插入的UI操作一定会在删/改UI操作后进行,如论代码的顺序是如何写的
  2. 同时进行删操作很可能会产生问题
  3. 插入是操作依赖更改后的数据源,而删操作依赖更改前的数据源

具体内容见上面的参考博客


更改cell的高度

这里单独把这块拎出来是因为需求用到了

直接reloadData

直接reloadData没有动画

begin/endUpdates

有隐式动画:

[tableView beginUpdates]; [tableView endUpdates];

前言

之前的需求一直专注于有关Feed的底层逻辑,对TableView/CollectionView的UI层的系统接口还是比较生疏,尤其是刷新部分

reloadData

reloadData是最常用的方法,调用reloadData后会调用一系列方法(顺序并不是下面所列的顺序,下面所列的方法也不全

  • numberOfSectionsInTableView
  • (NSInteger)numberOfRowsInSection:(NSInteger)section;
  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
  • (void)tableView:(UITableView )tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath)indexPath API_AVAILABLE(ios(6.0));

第一个坑就是reloadData是一个异步方法(主线程异步),也就是说执行了[tableView reloadData]会马上执行下面的代码,但此时列表并没有刷新完;

reloadData也不是开始调用就Async了,其上面所列的方法有同步和异步之分:

同步方法;numberOfSectionsInCollectionView 和 numberOfItemsInSection
异步方法:cellForItemAtIndexPath 和 heightForRowAtIndexPath

要想确保reloadData把列表刷新好了再跑下面的代码,有两种解决方法

  1. 调用layoutIfNeeded强绘
  2. 调用async-main,把后面代码也抛到异步

下面给大🔥整个Demo瞧瞧:

[tableView reloadData]; NSLog(@"data has been reload");

通过打日志输出如下:

2020-09-12 16:19:52.633305+0800 FeedDemo[56142:10761058] numberOfSectionsInTableView 2020-09-12 16:19:52.633523+0800 FeedDemo[56142:10761058] numberOfRowsInSection 2020-09-12 16:19:52.633709+0800 FeedDemo[56142:10761058] data has been reload  # 注意这里 2020-09-12 16:19:52.633891+0800 FeedDemo[56142:10761058] cellForRowAtIndexPath 2020-09-12 16:19:52.634045+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.634294+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.634488+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.634682+0800 FeedDemo[56142:10761058] cellForRowAtIndexPath 2020-09-12 16:19:52.634799+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.634971+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.635164+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.635336+0800 FeedDemo[56142:10761058] cellForRowAtIndexPath 2020-09-12 16:19:52.635530+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.635693+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.636064+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath

上面的日志正好说明了刚刚提到的两点:

  1. numberOfSectionsInTableView & numberOfRowsInSection 是同步调用,而heightForRowAtIndexPath & cellForRowAtIndexPath是异步调用
  2. 紧接着reloadData的代码会在列表没刷新完就马上调用

下面使用【强绘】的方式解决问题:

[tableView reloadData]; [tableView layoutIfNeeded]; NSLog(@"data has been reload");

日志如下:

2020-09-12 16:25:48.583714+0800 FeedDemo[56471:10766765] numberOfSectionsInTableView 2020-09-12 16:25:48.583883+0800 FeedDemo[56471:10766765] numberOfRowsInSection 2020-09-12 16:25:48.584084+0800 FeedDemo[56471:10766765] cellForRowAtIndexPath 2020-09-12 16:25:48.584216+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.584394+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.584563+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.584773+0800 FeedDemo[56471:10766765] cellForRowAtIndexPath 2020-09-12 16:25:48.584959+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.585113+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.585373+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.585598+0800 FeedDemo[56471:10766765] cellForRowAtIndexPath 2020-09-12 16:25:48.585710+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.585896+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.586069+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.586428+0800 FeedDemo[56471:10766765] data has been reload # 跑到最后了

再使用async-main的方式解决问题:

[tableView reloadData];     dispatch_async(dispatch_get_main_queue(), ^{         NSLog(@"data has been reload");     });

日志输出:

2020-09-12 16:28:49.432725+0800 FeedDemo[58104:10774035] numberOfSectionsInTableView 2020-09-12 16:28:49.432884+0800 FeedDemo[58104:10774035] numberOfRowsInSection 2020-09-12 16:28:49.433105+0800 FeedDemo[58104:10774035] cellForRowAtIndexPath 2020-09-12 16:28:49.433254+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.433512+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.433694+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.433878+0800 FeedDemo[58104:10774035] cellForRowAtIndexPath 2020-09-12 16:28:49.434090+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.434233+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.434467+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.434686+0800 FeedDemo[58104:10774035] cellForRowAtIndexPath 2020-09-12 16:28:49.434817+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.434973+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.435136+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.435930+0800 FeedDemo[58104:10774035] data has been reload # 也是在最后

Updates

下面根据这篇参考博客做一下回顾和总结

我以Updates为标题主要包括了以下内容:

- (void)beginUpdates  - (void)endUpdates - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;  - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation; - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation; - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

iOS11 又提供了begin/endUpdates的替代版performBatchUpdates

首先区分一点”数据源更改“和”UI更改“是两部操作

比如删除一行:

[self.tempArray[0] removeObjectAtIndex:1]; [self.tableView deleteRowsAtIndexPaths: indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];

都是先把数据源更改后再进行UI更改

begin/endUpdates主要是为了同时进行多个增删改(reload)操作,但要注意的是:

  1. begin/endUpdates内插入的UI操作一定会在删/改UI操作后进行,如论代码的顺序是如何写的
  2. 同时进行删操作很可能会产生问题
  3. 插入是操作依赖更改后的数据源,而删操作依赖更改前的数据源

具体内容见上面的参考博客


更改cell的高度

这里单独把这块拎出来是因为需求用到了

直接reloadData

直接reloadData没有动画

begin/endUpdates

有隐式动画:

[tableView beginUpdates]; [tableView endUpdates];

前言

之前的需求一直专注于有关Feed的底层逻辑,对TableView/CollectionView的UI层的系统接口还是比较生疏,尤其是刷新部分

reloadData

reloadData是最常用的方法,调用reloadData后会调用一系列方法(顺序并不是下面所列的顺序,下面所列的方法也不全

  • numberOfSectionsInTableView
  • (NSInteger)numberOfRowsInSection:(NSInteger)section;
  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
  • (void)tableView:(UITableView )tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath)indexPath API_AVAILABLE(ios(6.0));

第一个坑就是reloadData是一个异步方法(主线程异步),也就是说执行了[tableView reloadData]会马上执行下面的代码,但此时列表并没有刷新完;

reloadData也不是开始调用就Async了,其上面所列的方法有同步和异步之分:

同步方法;numberOfSectionsInCollectionView 和 numberOfItemsInSection
异步方法:cellForItemAtIndexPath 和 heightForRowAtIndexPath

要想确保reloadData把列表刷新好了再跑下面的代码,有两种解决方法

  1. 调用layoutIfNeeded强绘
  2. 调用async-main,把后面代码也抛到异步

下面给大🔥整个Demo瞧瞧:

[tableView reloadData]; NSLog(@"data has been reload");

通过打日志输出如下:

2020-09-12 16:19:52.633305+0800 FeedDemo[56142:10761058] numberOfSectionsInTableView 2020-09-12 16:19:52.633523+0800 FeedDemo[56142:10761058] numberOfRowsInSection 2020-09-12 16:19:52.633709+0800 FeedDemo[56142:10761058] data has been reload  # 注意这里 2020-09-12 16:19:52.633891+0800 FeedDemo[56142:10761058] cellForRowAtIndexPath 2020-09-12 16:19:52.634045+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.634294+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.634488+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.634682+0800 FeedDemo[56142:10761058] cellForRowAtIndexPath 2020-09-12 16:19:52.634799+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.634971+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.635164+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.635336+0800 FeedDemo[56142:10761058] cellForRowAtIndexPath 2020-09-12 16:19:52.635530+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.635693+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath 2020-09-12 16:19:52.636064+0800 FeedDemo[56142:10761058] heightForRowAtIndexPath

上面的日志正好说明了刚刚提到的两点:

  1. numberOfSectionsInTableView & numberOfRowsInSection 是同步调用,而heightForRowAtIndexPath & cellForRowAtIndexPath是异步调用
  2. 紧接着reloadData的代码会在列表没刷新完就马上调用

下面使用【强绘】的方式解决问题:

[tableView reloadData]; [tableView layoutIfNeeded]; NSLog(@"data has been reload");

日志如下:

2020-09-12 16:25:48.583714+0800 FeedDemo[56471:10766765] numberOfSectionsInTableView 2020-09-12 16:25:48.583883+0800 FeedDemo[56471:10766765] numberOfRowsInSection 2020-09-12 16:25:48.584084+0800 FeedDemo[56471:10766765] cellForRowAtIndexPath 2020-09-12 16:25:48.584216+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.584394+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.584563+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.584773+0800 FeedDemo[56471:10766765] cellForRowAtIndexPath 2020-09-12 16:25:48.584959+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.585113+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.585373+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.585598+0800 FeedDemo[56471:10766765] cellForRowAtIndexPath 2020-09-12 16:25:48.585710+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.585896+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.586069+0800 FeedDemo[56471:10766765] heightForRowAtIndexPath 2020-09-12 16:25:48.586428+0800 FeedDemo[56471:10766765] data has been reload # 跑到最后了

再使用async-main的方式解决问题:

[tableView reloadData];     dispatch_async(dispatch_get_main_queue(), ^{         NSLog(@"data has been reload");     });

日志输出:

2020-09-12 16:28:49.432725+0800 FeedDemo[58104:10774035] numberOfSectionsInTableView 2020-09-12 16:28:49.432884+0800 FeedDemo[58104:10774035] numberOfRowsInSection 2020-09-12 16:28:49.433105+0800 FeedDemo[58104:10774035] cellForRowAtIndexPath 2020-09-12 16:28:49.433254+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.433512+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.433694+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.433878+0800 FeedDemo[58104:10774035] cellForRowAtIndexPath 2020-09-12 16:28:49.434090+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.434233+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.434467+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.434686+0800 FeedDemo[58104:10774035] cellForRowAtIndexPath 2020-09-12 16:28:49.434817+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.434973+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.435136+0800 FeedDemo[58104:10774035] heightForRowAtIndexPath 2020-09-12 16:28:49.435930+0800 FeedDemo[58104:10774035] data has been reload # 也是在最后

Updates

下面根据这篇参考博客做一下回顾和总结

我以Updates为标题主要包括了以下内容:

- (void)beginUpdates  - (void)endUpdates - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;  - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation; - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation; - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

iOS11 又提供了begin/endUpdates的替代版performBatchUpdates

首先区分一点”数据源更改“和”UI更改“是两部操作

比如删除一行:

[self.tempArray[0] removeObjectAtIndex:1]; [self.tableView deleteRowsAtIndexPaths: indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];

都是先把数据源更改后再进行UI更改

begin/endUpdates主要是为了同时进行多个增删改(reload)操作,但要注意的是:

  1. begin/endUpdates内插入的UI操作一定会在删/改UI操作后进行,如论代码的顺序是如何写的
  2. 同时进行删操作很可能会产生问题
  3. 插入是操作依赖更改后的数据源,而删操作依赖更改前的数据源

具体内容见上面的参考博客


更改cell的高度

这里单独把这块拎出来是因为需求用到了

直接reloadData

直接reloadData没有动画

begin/endUpdates

有隐式动画:

[tableView beginUpdates]; [tableView endUpdates];

部分转自互联网,侵权删除联系

赞(0) 打赏
部分文章转自网络,侵权联系删除b2bchain区块链学习技术社区 » 关于TableView刷新的那些事求职学习资料
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

b2b链

联系我们联系我们