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

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

问题描述

我正在尝试使用 UITableView 来模仿 iMessage 气泡文本行为.为了始终滚动到底部,我在 viewDidLoadviewDidAppear 时使用了 scrollToRowAtIndexPath.这是因为当调用 viewDidLoad 方法时,表格还没有完全加载,所以我需要在 viewDidAppear 中额外滚动.这段代码成功了.但是,我想要的不是动画滚动(将 animated 设置为 NO 并不能解决此问题),我希望表格始终从底部显示,而不是加载表,然后转到最后一行.

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天全站免登陆