我們知道 tableview 刷新有分全局刷新和指定區(qū)域刷新。
-
全局刷新
- (void)reloadData; -
指定區(qū)域刷新有以下兩個方法。
- (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動畫,有時視覺上會有閃一下的情況。指定區(qū)域刷新時,只要將UITableViewRowAnimation設為UITableViewRowAnimationNone即可取消隱式動畫。
那么全局刷新時,該如何取消隱式動畫?
方法一
如果你的tableview的行高是根據(jù)數(shù)據(jù)自適應的,那么在設置完estimatedRowHeight后,在需要reloadData的地方加上。
[self.tableView beginUpdates];
[self.tableView endUpdates];
(ps: 以上方法也適用于 tableview 刷新時因預估行高和實際行高不一致情況下抖動的問題,有些人視覺上可能也覺得會閃一下。)
方法二
使用UIView類方法去取消隱式動畫,在 block 回調(diào)里去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