UITableView在单元格刷新时滚动到顶部 [英] UITableView Scrolls to Top on Cell Refresh

查看:403
本文介绍了UITableView在单元格刷新时滚动到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,其单元格的动态大小适合其中的UITextView。每当键入一个键时,单元格会检查计算出的高度是否增加,如换行符一样,因此它可以告诉表格需要重新计算单元格的高度。我用这段代码做到了。

I have a UITableView with a cell that is dynamically sized to fit a UITextView inside it. Whenever a key is typed, the cell checks to see if the calculated height has increased, as with a newline, so it can tell the table the the cell's height needs to be recalculated. I do that with this code.

- (void) createNoteCellViewDidUpdate: (CreateNoteCell*) cell {

    CGFloat newHeight = [self tableView:self.tableView heightForRowAtIndexPath:CreateNoteCellIndexPath];
    if (newHeight != CGRectGetHeight(cell.contentView.frame)) {
        [self.tableView beginUpdates];
        [self.tableView endUpdates]; // <- HERE IS WHERE THE CONTENT OFFSET CHANGES
    }
}

调整大小和表格范围符合预期。但它不仅仅是动画高度的变化,而是滚动到顶部。按下返回键时可以看到这一点。

The resize and table bounds are as expected. But instead of just animating the change in height, it also scrolls to the top. This can be seen here, when pressing the return key.

按键值观察,我发现滚动视图的contentSize更改时正在设置滚动视图contentOffset。并且在调用endUpdates方法时,contentSize会多次更改。当它改变时,它从正常高度变为相当大的高度,然后回到正常高度。我想知道它是否会因此而上升。我不知道从哪里开始。

By Key Value Observing, I've found out that the scroll view contentOffset is being set when the scroll view's contentSize changes. And that the contentSize changes many times when the endUpdates method is called. When it changes it goes from a normal height, to a rather large height, then back to a normal height. I wonder if it's going to top because of that. I don't know where to go from here.

推荐答案

哇,Josh Gafni的回答很有帮助;他值得一个赞成。我的解决方案是基于他的。这仍然感觉像是一个黑客,所以如果有人还有更好的方法来做到这一点,我仍然很有兴趣听到它。

Wow, Josh Gafni's answer helped a lot; he deserves an upvote. My solution is based off his. This still feels like a hack, so if anyone still has a better way to do this, I'm still very interested to hear it.

首先我保存内容偏移,然后我开始更新。

First I save the content offset, then I start the update.

CGPoint offset = self.tableView.contentOffset;
[self.tableView beginUpdates];
[self.tableView endUpdates];

这会动画单元格的高度变化并调整所有帧以适应约束。

This animates the cell's height change and adjusts all the frames to suit the constraints.

接下来我从tableView的图层中删除所有动画,因此顶部的动画停止。然后我将contentOffst重置为之前的状态。

Next I remove all animations from the tableView's layer, so the animation to the top stops. Then I reset the contentOffst to what it was before.

[self.tableView.layer removeAllAnimations];
[self.tableView setContentOffset:offset animated:NO];

最后,我让tableView处理动画向下滚动,以便单元格的底部位于键盘。

Finally, I let the tableView handle the animated scroll down so the bottom of the cell is right above the keyboard.

[self.tableView scrollToRowAtIndexPath:CreateNoteCellIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

瞧,它运作得很好。

这篇关于UITableView在单元格刷新时滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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