我有一个设置页面,其中包含带有复选标记附件的长列表。用户可以根据需要选择任意数量的行,并将其保存在NSUserDefaults中。我想在下次打开设置时选择他们之前的选择,所以我试过这个:
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.
我做错了什么?感谢
答案 0 :(得分:0)
错误很明确。 UIKit中的NSIndexPaths需要两个索引,一个用于该部分,一个索引用于该行。这是explained here
假设您的表格视图中只有一个部分:
for (id obj in array) {
NSIndexPath *path = [NSIndexPath indexPathForRow:obj inSection:0];
[self.tableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionTop];
}