如何在最后一个单元格上启动 UITableView? [英] How to Start UITableView on the Last Cell?

查看:21
本文介绍了如何在最后一个单元格上启动 UITableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Apple 的 Messages 应用程序中,当您单击通讯员的姓名并切换到对话的表格视图(每条消息都有气球)时,表格会一直滚动到最后.没有动画或任何东西,它就在那里.

In Apple's Messages app, when you click a correspondent's name and switch to the table view of the conversation (with balloons for each message), the table appears scrolled all the way to the end. No animation or anything, it's just there.

类似地,在 Tweetie 2 中,当您加载推文视图时,它就会出现在您上次查看它的位置.没有动画到达那里,它就在那里,好像上面的单元格都没有被加载.

Similarly, in Tweetie 2, when you load the tweets view, it appears right where you last looked at it. No animation to get there, it's just there, as if none of the cells above were loaded.

这些应用是如何做到这一点的?他们是否在表控制器的某处调用 scrollToRowAtIndexPath:atScrollPosition:animated: ?如果是这样,他们如何知道要传递给 atScrollPosition: 的内容?又是用什么方法调用的?

How do these apps do this? Are they calling scrollToRowAtIndexPath:atScrollPosition:animated: somewhere in the table controller? If so, how do they know what to pass to atScrollPosition:? And in what method is it called?

推荐答案

scrollToRowAtIndexPath 应该可以工作.

viewWillAppear: 中,试试这个:

[theTableView reloadData];    
NSIndexPath* ip = [NSIndexPath indexPathForRow:rowNumberHere inSection:sectionNumberHere];
[theTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];

rowNumberHere 是您要滚动到的数据源中的行号.

rowNumberHere is the row number in the data source you want to scroll to.

atScrollPosition 只是 UITableViewScrollPosition 枚举中的值之一,它可以确定您想要的行号将显示在屏幕上的哪个位置.但是,根据行数和滚动到的行,它可能没有区别.

atScrollPosition is just one of the values in the UITableViewScrollPosition enum which can determine where on the screen the row number you want will show up. However, depending on the number of rows and which row you are scrolling to, it may not make a difference.

Putting reloadData: 可避免在 viewWillAppear: 中尚未加载数据时出现异常.如果你把 scrollToRowAtIndexPath 放在 viewDidAppear: 中,你就不需要 reloadData: 但你会看到表格跳了一点你说的不想.

Putting reloadData: avoids an exception if the data is not loaded yet in viewWillAppear:. If you put the scrollToRowAtIndexPath in viewDidAppear:, you would not need the reloadData: but you will see the table jump a little which you say you don't want.

@Theory,请尝试按如下方式更改您的代码...

@Theory, try changing your code as follows...

[tableView reloadData];
int lastRowNumber = [tableView numberOfRowsInSection:0] - 1;
NSIndexPath* ip = [NSIndexPath indexPathForRow:lastRowNumber inSection:0];
[tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];

请注意 numberOfRowsInSection 返回行数,而不是最后一行数(即行数 - 1).

Please note numberOfRowsInSection returns row count, not the last row number (which is row count - 1).

这篇关于如何在最后一个单元格上启动 UITableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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