调用 reloadRowsAtIndexPaths 时 UITableView 弹回到部分的顶部 [英] UITableView bouncing back to the top of a section when calling reloadRowsAtIndexPaths

查看:18
本文介绍了调用 reloadRowsAtIndexPaths 时 UITableView 弹回到部分的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击我的一行中的按钮时,我正在更新该行的基础模型,然后为给定行调用 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];
    }
}

问题是table view在进行这个调用后弹回到了section的顶部.如何防止这种情况发生并保持滚动位置不变?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆