用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];
}];

 

 

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