用reloadRowsAtIndexPaths、reloadSections 刷新tableView時跳動問題

一個很常見的需求就是在一個cell上點贊,評論等操作時,需要刷新單個cell對象,常用的方法即爲:

  [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:index inSection:0],nil] withRowAnimation:UITableViewRowAnimationNone];

僅僅這行代碼會引起cell上下跳動的問題,原因是 上述刷新過程中,雖然我們已經設置UITableViewRowAnimationNone,但任然會默認添加隱式動畫效果。 解決辦法:去掉動畫效果。

方法1:(支持iOS7+)

  [UIView performWithoutAnimation:^{
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:index inSection:0],nil] withRowAnimation:UITableViewRowAnimationNone];
        }];

方法2:

[UIView animateWithDuration:0 animations:^{
    [collectionView performBatchUpdates:^{
        [collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
    } completion:nil];
}];

方法3:(僅用於iOS11以後)

[UIView setAnimationsEnabled:NO];
[self.tableView performBatchUpdates:^{
     [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:index inSection:0],nil] withRowAnimation:UITableViewRowAnimationNone];
   } completion:^(BOOL finished) {
      [UIView setAnimationsEnabled:YES];
}];

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章