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

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

问题描述

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

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.

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