一个很常见的需求就是在一个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];
}];
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有