Load UITableView from the bottom(从底部加载 UITableView)
问题描述
我正在尝试使用 UITableView
来模仿 iMessage 气泡文本行为.为了始终滚动到底部,我在 viewDidLoad
和 viewDidAppear
时使用了 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从底部加载 UITableView
- Android viewpager检测滑动超出范围 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- android 4中的android RadioButton问题 2022-01-01