我們知道 tableview 刷新有分全局刷新和指定區域刷新。
-
全局刷新
- (void)reloadData;
-
指定區域刷新有以下兩個方法。
- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
tableview
或者是collectionview
,reload
時默認會有一個隱式的Fade
動畫,有時視覺上會有閃一下的情況。指定區域刷新時,只要將UITableViewRowAnimation
設爲UITableViewRowAnimationNone
即可取消隱式動畫。
那麼全局刷新時,該如何取消隱式動畫?
方法一
如果你的tableview
的行高是根據數據自適應的,那麼在設置完estimatedRowHeight
後,在需要reloadData
的地方加上。
[self.tableView beginUpdates];
[self.tableView endUpdates];
(ps: 以上方法也適用於 tableview 刷新時因預估行高和實際行高不一致情況下抖動的問題,有些人視覺上可能也覺得會閃一下。)
方法二
使用UIView
類方法去取消隱式動畫,在 block 回調裏去reloadData
。該方法對collectionview
刷新同樣有效。
+ (void)performWithoutAnimation:(void (NS_NOESCAPE ^)(void))actionsWithoutAnimation NS_AVAILABLE_IOS(7_0);
參考鏈接:https://stackoverflow.com/questions/15196927/reload-uitableview-with-new-data-caused-flickering