喜 我正在尝试在nsuserdefault中保存勾选行的状态。有些我如何得到错误 :setObject undeclared。
我的代码是:
- (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方法中获取行状态(我应该在这里获取吗?在视图中加载)。我的代码如下:
- (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 unclared。
我知道为什么这些错误即将来临,因为它们(indexpath和cell)都不在范围内,然后放置此代码的位置(nsuserdefaults data retriving code)。请指导我的。 谢谢!
答案 0 :(得分:0)
尝试替换它:
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"];