从底部加载UITableView [英] Load UITableView from the bottom

查看:100
本文介绍了从底部加载UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UITableView 来模仿iMessage气泡文本行为。为了始终滚动到底部,当 viewDidLoad viewDidAppear <时,我正在使用 scrollToRowAtIndexPath / code>。这是因为当调用 viewDidLoad 方法时,该表尚未完全加载,因此我需要 viewDidAppear 。这段代码就是诀窍。但是,我想要的不是动画卷轴(将动画设置为无法解决此问题),我想要要始终从底部显示的表,不加载表然后转到最后一行。

I'm trying to mimic the iMessage bubble text behaviour with an UITableView. In order to always scroll to the bottom I'm using scrollToRowAtIndexPath when viewDidLoad and viewDidAppear. This is because when the viewDidLoad method is called, the table has not been completely loaded, so I need that extra scroll in viewDidAppear. This code makes the trick. However, what I want is not an animated scroll (setting animated to NO does not solve this), I want the table to be displayed always from the bottom, not load the table and then go to the last row.

这可能吗?我找不到任何完全符合所需行为的解决方案。

Is this possible? I can't find any solution that fits completely with the desired behaviour.

推荐答案

您可以避免来自viewDidLoad的调用,因为滚动来自在viewDidAppear中,第一次调用是多余的。每次导航回视图时都会调用viewDidAppear,但只在初始化视图时调用viewDidLoad一次。

You can avoid the call from viewDidLoad because scrolling from within viewDidAppear makes that first call redundant. viewDidAppear is called every time you navigate back to the view but viewDidLoad is only called once when the view is initialized.

我同意早先从用户隐藏滚动的建议,而不是改变UITableView加载数据的方式。我的建议是在viewWillAppear方法中使用scrollToRowAtIndexPath方法,并将动画设置为NO。之后,如果您必须在表对用户可见时添加新行,请使用insertRowsAtIndexPaths:withRowAnimation:在表视图的底部添加一行。务必在数据模型的末尾添加数据,以便当用户导航并返回时,他/她将返回相同的布局。

I would agree with earlier suggestions of hiding the scroll from the user instead of changing the way a UITableView is loading data. My suggestion would be to use the scrollToRowAtIndexPath method in the viewWillAppear method with animation set to NO. After that if you have to add a new row while the table is visible to the user, use insertRowsAtIndexPaths:withRowAnimation: to add a row at the bottom of the table view. Be sure to take care of adding the data at the end of your data model so that when the user navigates away and comes back, s/he comes back to the same layout.

希望这会有所帮助。

编辑:
刚刚看到你不接受以前答案的理由并且以为我会详细说明一点。我建议的解决方案需要尽可能少的工作,避免一次又一次地调用reloadData,从而避免一次又一次地调用scrollToRowAtIndexPath方法。你只需要在viewWillAppear中调用一个scrollToRowAtIndexPath来滚动到表视图的底部(这样做时隐藏了用户的转换),你就不需要再这样做了。

edit: Just saw your reason for not accepting the previous answers and thought I'd elaborate a little more. The solution I propose would require minimum effort, avoid calling reloadData time and again and thus avoid calling the scrollToRowAtIndexPath method again and again. You only need to make one call to scrollToRowAtIndexPath in viewWillAppear to scroll to the bottom of the table view (hiding the transition from the user when doing so) and you wouldn't need to do that again.

这篇关于从底部加载UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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