gpt4 book ai didi

ios - 调用 reloadRowsAtIndexPaths 时 UITableView 弹回部分顶部

转载 作者:IT老高 更新时间:2023-10-28 11:48:12 25 4
gpt4 key购买 nike

当用户点击我的某一行中的按钮时,我正在更新该行的基础模型,然后为给定行调用 reloadRowsAtIndexPaths(即单行重新加载)。

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

问题是表格 View 在进行此调用后反弹回该部分的顶部。如何防止这种情况发生并保持滚动位置不变?

最佳答案

啊哈!我找到了问题,我将为将来遇到这个问题的可怜人回答我自己的问题。

我所有的单元格的高度都是可变的,所以我在 UITableViewDelegate 中使用了新的 iOS7 方法,认为它可能会加快渲染时间(并不是我真的需要它):

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;

不管怎样,实现这个方法有一个恶毒的副作用,就是调用的时候表格会反弹到section的顶部:

[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

为了解决弹跳问题,我刚刚删除了估计的HeightForRowAtIndexPath 方法的覆盖,现在一切正常。终于开心了。

关于ios - 调用 reloadRowsAtIndexPaths 时 UITableView 弹回部分顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24767665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com