简体   繁体   English

恢复滚动位置uitableview

[英]restore scroll position uitableview

I have a table view that leads to a detail view.我有一个表视图,可通向详细信息视图。 When I click back from the table view I return to the top of the table.当我从表视图中单击返回时,我将返回到表的顶部。 I would like it to return to the position of the selected row.我希望它返回到所选行的位置。 I know I can use scroll to position but it needs an index path.我知道我可以使用滚动定位,但它需要一个索引路径。 Am I right in thinking I need to create a index path property and use that in the method call.我认为我需要创建一个索引路径属性并在方法调用中使用它是否正确。 If so, where do I populate it - in the did select row/cell method?如果是这样,我在哪里填充它 - 在 did select row/cell 方法中? I have sections in my table so would it be index path section, index path row?我的表中有部分,所以它是索引路径部分,索引路径行吗? And finally is it to be called in view did appear?最后它被称为视图确实出现了?

Maybe u looking for this:也许你正在寻找这个:

tableView.contentOffset.y;

If you mean - you back from detail controller to ur master controller, u can scroll to position in viewWillAppear method (save tableView.contentOffset.y in viewWillDissapear).如果你的意思是 - 你从细节控制器回到你的主控制器,你可以滚动到 viewWillAppear 方法中的位置(在 viewWillDissapear 中保存 tableView.contentOffset.y)。 But i created new project and there no need to do this, it automatically open master controller with saved position when back from detail但是我创建了新项目并且不需要这样做,它会在从细节返回时自动打开带有保存位置的主控制器

It should work using - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated using it this way:它应该使用- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated以这种方式工作:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:selectedRow inSection:yourSection];
[yourTableView scrollToRowAtIndexPath:indexPath 
                     atScrollPosition:UITableViewScrollPositionTop 
                             animated:YES];

atScrollPosition could take any of these values: atScrollPosition可以采用以下任何值:

typedef enum {
    UITableViewScrollPositionNone,
    UITableViewScrollPositionTop,
    UITableViewScrollPositionMiddle,
    UITableViewScrollPositionBottom
} UITableViewScrollPosition;

I hope this helps you我希望这可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM