gpt4 book ai didi

iphone - UITableViewscrollToRowAtIndexPath

转载 作者:行者123 更新时间:2023-12-03 18:19:40 25 4
gpt4 key购买 nike

我试图让我的表格 View 记住它的最后位置,以便应用程序再次运行后,它会滚动到该位置。为此,在我的 viewWillDisappear: 方法中,我获取第一个可见行号并将其保存到 NSUserDefaults 中。然后在我的 viewDidAppear 中,我尝试通过将 scrollToRowAtIndexPath 发送到 TableView 来滚动到该行。但是我得到一个 NSRangeException:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]: section (0) beyond bounds (0).

任何帮助表示赞赏。这是代码:

- (void)viewDidAppear:(BOOL)animated
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

if ([userDefaults valueForKey:@"content_row"] != nil)
{
int rowToHighlight = [[userDefaults valueForKey:@"content_row"] intValue];
NSIndexPath * ndxPath= [NSIndexPath indexPathForRow:rowToHighlight inSection:0];
[contentTableView scrollToRowAtIndexPath:ndxPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}

-(void) viewWillDisappear:(BOOL)animated
{
NSIndexPath *selectedRowPath = [[contentTableView indexPathsForVisibleRows] objectAtIndex:0];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:selectedRowPath.row forKey:@"content_row"];
}

最佳答案

在调用scrollToRowAtIndexPath之前重新加载TableView数据。

[contentTableView reloadData];

关于iphone - UITableViewscrollToRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1480092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com