【发布时间】:2023-04-02 18:30:01
【问题描述】:
当用户点击我的某一行中的按钮时,我正在更新该行的基础模型,然后为给定行调用 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];
}
}
问题是表格视图在进行此调用后反弹回该部分的顶部。如何防止这种情况发生并保持滚动位置不变?
【问题讨论】:
-
代码看起来不错。你能分享更多与问题相关的代码吗?如果您的视图控制器调用
[self.tableView reloadData];,代码中是否有任何位置? -
我添加了更多代码来展示整个方法。一旦调用 reloadRowsAtIndexPaths,表格会自动滚动到节的顶部,但仅当节标题离开屏幕时。单元格创建或节标题(只是一个字符串)没有什么特别之处。
-
@Keenle 我没有执行任何 reloadData 或类似的操作。此外,文档中没有任何关于弹跳到 reloadRows 部分顶部的内容。我以前从未注意到此功能,但我通常不会在单元格本身内进行数据更改操作,因此这对我来说是一种新型交互方式。
-
尝试检查从
indexPathForView收到的indexPath的值。可以是nil。如果是这样,它一定会导致滚动到顶部。 -
@Keenle indexPath 是正确的。这是我检查的第一件事。此外,它不会反弹到表的顶部 (0,0)。它反弹到部分的顶部 (1,0)。
标签: ios objective-c uitableview ios7