Ios 选择viewdidload上已保存的选中标记行
我有一个设置页面,其中有一个带有复选标记附件的长列表。用户可以选择任意多的行,我让他们保存在NSUserDefaults中。我希望下次打开“设置”时选择他们以前的选择,因此我尝试了以下方法:Ios 选择viewdidload上已保存的选中标记行,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我有一个设置页面,其中有一个带有复选标记附件的长列表。用户可以选择任意多的行,我让他们保存在NSUserDefaults中。我希望下次打开“设置”时选择他们以前的选择,因此我尝试了以下方法: NSArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"choices"]]; if (array.count !=0) { NSLog(@"not
NSArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"choices"]];
if (array.count !=0) {
NSLog(@"not empt");
for (id obj in array) {
NSIndexPath *path = [NSIndexPath indexPathWithIndex:obj];
[self.tableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionTop];
}
}
但每次应用程序都会因以下错误而崩溃:
erminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.
我做错了什么?谢谢这个错误很容易解释。UIKit中的nsindexpath需要两个索引,一个用于节,一个用于行。它是 假设表视图中只有一个部分:
for (id obj in array) {
NSIndexPath *path = [NSIndexPath indexPathForRow:obj inSection:0];
[self.tableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionTop];
}
现在获取此-[UITableView\u ContentOffsetForScrollingToRowatineXPath:atScrollPosition:]:行(204883856)超出节(0)的边界(10)。'