Dav*_*ave 8 iphone scroll uitableview uiscrollview
我正在使用iPhone SDK 3.1.3.我有一个UITableViewController从另一个控制器获取数据.表视图作为子视图添加到主视图中,但框架已设置为不可见.更新表视图框,并通过点击按钮使其在主视图上滑动.
出现表格视图,我滚动到最后一行.如果我选择最后一行,我会用更多数据重新加载表.该表随着更多数据而更新.除了滚动位置始终是顶部之外,一切正常.
我需要滚动位置是我点击以加载更多数据的最后一行.我保存滚动位置并在加载更多数据后调用以下代码.它执行没有问题,但滚动位置始终是顶部.
[theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:savedScrollPosition animated:NO];
Run Code Online (Sandbox Code Playgroud)
以上似乎没有效果.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];
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*ave 31
这似乎可以解决问题.
[theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
CGPoint point = theTableView.contentOffset;
point .y -= theTableView.rowHeight;
theTableView.contentOffset = point;
Run Code Online (Sandbox Code Playgroud)
dra*_*ard 10
它看起来会更好,如果你可以插入行而不是调用reloadData,滚动位置将保持不变.
[theTableView beginUpdates];
[theTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
// make sure the dataSource will return new rows before calling endUpdates
[theTableView endUpdates];
Run Code Online (Sandbox Code Playgroud)
您可以使用UIScrollView滚动而不是使用UITableView滚动:
savedOffset = [theTableView contentOffset];
Run Code Online (Sandbox Code Playgroud)
然后恢复:
[theTableView setContentOffset:savedOffset];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42962 次 |
| 最近记录: |