嗨 我正在尝试在 nsuserdefaults 中保存勾选行的状态。我是如何出错的 :setObject 未声明。
我的代码如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
NSInteger newRow = [indexPath row];
NSInteger oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
if(newRow != oldRow)
{
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
prefs = [setObject:lastIndexPath forKey:@"lastIndexPath"];
}
我也在尝试在 viewdidload 方法中获取行状态(我应该在这里获取吗?在 View 中加载)。我的代码如下:
- (void)viewDidLoad {
menuList=[[NSMutableArray alloc] initWithObjects:
[NSArray arrayWithObjects:@"LOCATION1",nil],
[NSArray arrayWithObjects:@"LOCATION2",nil],
[NSArray arrayWithObjects:@"LOCATION3",nil],
[NSArray arrayWithObjects:@"LOCATION4",nil],
[NSArray arrayWithObjects:@"LOCATION5",nil],
[NSArray arrayWithObjects:@"LOCATION6",nil],
[NSArray arrayWithObjects:@"LOCATION7",nil],
nil];
[self.navigationController setNavigationBarHidden:NO];
self.navigationController.navigationBar.tintColor=[UIColor blackColor];
self.navigationController.navigationBar.barStyle=UIBarStyleBlackTranslucent;
self.title=@"Location Selection";
[table reloadData];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if([[prefs objectForKey:@"lastIndexPath"] compare: indexPath]== NSOrderedSame){
cell.AccessoryType = UITableViewCellAccessoryCheckMark;
}
[super viewDidLoad];
}
并出现错误:indexPath Undeclared 和 cell undeclared。
我明白为什么会出现这些错误,因为它们(索引路径和单元格)都不在范围内,然后在哪里放置此代码(nsuserdefaults 数据检索代码)。请指导我。 谢谢!
最佳答案
尝试替换这个:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
prefs = [setObject:lastIndexPath forKey:@"lastIndexPath"];
有了这个:
[[NSUserDefaults standardUserDefaults] setObject:lastIndexPath forKey:@"lastIndexPath"];
您的错误是因为您在调用 setObject:forKey: 时没有指定您的 prefs 对象正在调用它。所以编译器没有将其识别为有效消息。您还试图将消息输出到您的 prefs 对象,这是不正确的。
这就是说,我很确定你不能直接在 plist 中存储 NSIndexPath,你要么必须存储为 NSNumber、NSString 或 NSData。
这看起来像这样:
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:lastIndexPath.row] forKey:@"lastIndexPath"];
关于iphone - 在 nsuserdefaults 中保存复选标记附件值,然后检索它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5581841/