UITableView 滚动到某个位置

我用 iPhone SDK 3.1.3. 我有
UITableViewController

, 从另一个控制器接收数据。 表格视图被添加为亚副本到主表示,但帧设置为不可见。 通过按下按钮,更新表视图帧并根据主表单进行幻灯片。

表格视图将出现,我 scroll 直到最后一行。 如果我选择最后一个字符串,我将重新启动具有大量数据的表。 该表已使用新数据进行更新。 除了那个位置,一切都很好 scroll 总是顶部。

我需要一个位置 scroll 它是我点击以下载更多数据的最后一个字符串。 我保留了这个位置 scroll 并在加载更多数据后调用以下代码。 它在没有问题的情况下进行,但位置 scroll 总是顶部。


[theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:savedScrollPosition animated:NO];


显然,上述情况无效。
ViewWillAppear:


ViewDidAppear:

它不起作用,他们说,如果在代码中创建了视图控制器,那么他们不起作用。 请帮助我弄清楚如何以及何时建立一个职位 scroll 重新启动表格后 /
[theTableView reloadData]

/, 所以它是我点击的行。

重新启动表格视图的代码 & 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];
}
已邀请:

喜特乐

赞同来自:

似乎已经完成了他的工作。


[theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
CGPoint point = theTableView.contentOffset;
point .y -= theTableView.rowHeight;
theTableView.contentOffset = point;

小姐请别说爱

赞同来自:

它看起来更好和位置 scroll 如果您可以插入线而不是调用,将保持修复 reloadData.


[theTableView beginUpdates];
[theTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
// make sure the dataSource will return new rows before calling endUpdates
[theTableView endUpdates];


而不是使用 UITableView 滚动您可以使用 UIScrollView 滚动:


savedOffset = [theTableView contentOffset];


然后恢复:


[theTableView setContentOffset:savedOffset];

卫东

赞同来自:

如果这是实际代码并假设 theTableView 没有零,你必须收到警告 "may not respond to...", 因为 scrollToRowAtIndexPath 写错。

其次,参数 atScrollPosition 预计
http://developer.apple.com/iph ... ition
清单 UITableViewScrollPosition, 指示要在屏幕上找到目标线的位置。

试试吧:


[theTableView scrollToRowAtIndexPath:
[NSIndexPath indexPathForRow:savedScrollPosition inSection:0]
atScrollPosition:UITableViewScrollPositionBottom
animated:NO];

要回复问题请先登录注册