UITableView滚动到特定位置
我正在使用 iPhone SDK 3.1.3。我有一个 UITableViewController 从另一个控制器获取数据。表视图作为子视图添加到主视图中,但框架被设置为不可见。通过点击按钮,表格视图框架会更新并在主视图上滑动。
表格视图出现,我滚动到最后一行。如果我选择最后一行,我会用更多数据重新加载表。该表会更新更多数据。除了滚动位置始终位于顶部之外,一切正常。
我需要滚动位置是我单击以加载更多数据的最后一行。我保存滚动位置并在加载更多数据后调用以下代码。它执行时没有问题,但滚动位置始终位于顶部。
[theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:savedScrollPosition animated:NO];
上面的好像没有什么效果。 ViewWillAppear:
ViewDidAppear:
不会触发,我被告知如果视图控制器在代码中实例化(这种情况),它们就不会触发。请帮助我弄清楚重新加载表格后如何以及何时设置滚动位置 ([theTableView reloadData]
),以便它位于我单击的行。
重新加载表视图的代码滚动
////performAction will notify the tableviewcontroller which will result in didPerformAction being called
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == lastRow)
{
savedScrollPosition = lastRow;
//perform the action
[controller performAction];
}
}
- (void) didPerformAction:(NSNotification *)obj
{
[theTableView reloadData];
[theTableView
scrollToRowAtIndexPath: [NSIndexPath indexPathForRow:savedScrollPosition inSection:0]
atScrollPosition:UITableViewScrollPositionBottom
animated:NO];
}
I am using iPhone SDK 3.1.3. I have a UITableViewController
which gets data from another controller. The table view is added as a subview to the mainview but the frame is set so that it is not visible. The table view frame is updated and made to slide over the main view by tapping on a button.
The table view appears and I scroll to the last row. If I select the last row, I reload the table with more data. The table gets updated with more data. Everything works fine except the scroll position is always the top.
I need the scroll position to be the last row that I clicked on to load more data. I save the scroll position and call the following code after it loads more data. It executes without issues but the scroll position is always the top.
[theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:savedScrollPosition animated:NO];
The above seems to have no effect. ViewWillAppear:
ViewDidAppear:
does not fire and I am told that if the view controller is instantiated in code, which is the case, these don't fire. Please help me figure out how and when to set the scroll position after the table is reloaded ([theTableView reloadData]
) so that it is at the row that I clicked on.
Code to Reload table view & scroll
////performAction will notify the tableviewcontroller which will result in didPerformAction being called
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == lastRow)
{
savedScrollPosition = lastRow;
//perform the action
[controller performAction];
}
}
- (void) didPerformAction:(NSNotification *)obj
{
[theTableView reloadData];
[theTableView
scrollToRowAtIndexPath: [NSIndexPath indexPathForRow:savedScrollPosition inSection:0]
atScrollPosition:UITableViewScrollPositionBottom
animated:NO];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎成功了。
This seemed to do the trick.
如果您可以插入行而不是调用 reloadData,它看起来会更好,并且滚动位置将保持固定。
您可以使用 UIScrollView 滚动代替使用 UITableView 滚动:
然后恢复:
It will look better and the scroll position will stay fixed if you can insert the rows instead of calling reloadData.
Instead of using UITableView scrolling you can use UIScrollView scrolling:
Then restore:
如果这是实际的代码,并且假设 theTableView 不为零,那么您应该会收到一条警告,指出“可能不会响应...”,因为scrollToRowAtIndexPath 拼写错误。
其次, atScrollPosition 参数需要 UITableViewScrollPosition 枚举值,指示目标行在屏幕上的位置。
试试这个:
If that's the actual code and assuming theTableView is not nil there, you should be getting a warning saying "may not respond to..." because scrollToRowAtIndexPath is spelled wrong.
Secondly, the atScrollPosition parameter expects a UITableViewScrollPosition enum value indicating where on the screen you want the target row to be positioned.
Try this: