调用reloadRowsAtIndexPaths时,UITableView反弹回到部分的顶部 [英] UITableView bouncing back to the top of a section when calling reloadRowsAtIndexPaths
问题描述
当用户点击我的一行中的按钮时,我正在更新该行的基础模型,然后调用给定行的reloadRowsAtIndexPaths(即单行重新加载)。
When a user taps a button in one of my rows I am updating the underlying model for that row and then calling reloadRowsAtIndexPaths for the given row (i.e. single row reload).
- (IBAction)handleCompleteTouchEvent:(UIButton *)sender {
NSIndexPath *indexPath = [self.tableView indexPathForView:sender];
id item = [self dataForIndexPath:indexPath];
if ([item respondsToSelector:@selector(completed)]) {
// toogle completed value
BOOL completed = ![[item valueForKey:@"completed"] boolValue];
[item setValue:[NSNumber numberWithBool:completed] forKey:@"completed"];
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
}
问题是表格视图反弹回拨打此电话后,该部分的顶部。如何防止这种情况发生并将滚动位置保持在原位?
The problem is that the table view bounces back to the top of the section after making this call. How can I prevent this from occurring and keep the scroll position where it is?
推荐答案
啊哈!我找到了问题,我将回答自己的问题,为将来遇到这个问题的可怜的灵魂。
Ah Ha! I found the problem and am going to answer my own question for the poor soul who runs into this issue in the future.
我的所有细胞都有不同的高度,所以我是在UITableViewDelegate中使用新的iOS7方法,认为它可能会加快渲染时间(不是我真的需要它):
All of my cells have variable height so I was using the new iOS7 method in UITableViewDelegate thinking it might speed up render time (not that I really needed it):
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;
无论如何,实现这种方法会产生邪恶的副作用,导致表格反弹到顶部调用时的部分:
Anyway, implementing this method has the evil side effect of causing the table to bounce to the top of the section when calling:
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
为了解决跳出问题,我刚刚删除了estimatedHeightForRowAtIndexPath方法的覆盖,现在一切正常。终于快乐了。
To solve the bounce problem I just removed the override of that estimatedHeightForRowAtIndexPath method and now everything works as it should. Happy at last.
这篇关于调用reloadRowsAtIndexPaths时,UITableView反弹回到部分的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!