Iphone 在nsuserdefaults中保存选中标记附件值,然后重新修改它们

Iphone 在nsuserdefaults中保存选中标记附件值,然后重新修改它们,iphone,objective-c,ios,Iphone,Objective C,Ios,嗨 我试图在nsuserdefaults中保存勾选行的状态。我是如何得到错误的 :setObject未声明 我的代码如下: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; NSInteger n

嗨 我试图在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"];

}
- (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];
}  
另外,我正在尝试在viewdidload方法中获取行状态。我应该在此处获取吗?在ViewDid load中。我的代码如下:

- (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"];

}
- (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和未声明的cell

我明白了为什么会出现这些错误,因为mindexpath和cell都不在范围内,然后将此代码放置在何处。请指导我的操作。 谢谢

尝试替换此:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
prefs = [setObject:lastIndexPath forKey:@"lastIndexPath"];
为此:

[[NSUserDefaults standardUserDefaults] setObject:lastIndexPath forKey:@"lastIndexPath"];
错误是因为调用setObject:forKey:时未指定prefs对象正在调用它。因此编译器没有将其标识为有效消息。您还试图将消息输出分配到prefs对象,这是不正确的

这就是说,我非常确定您不能直接在plist中存储nsindepath,您必须将其存储为NSNumber、NSString或NSData

这看起来像这样:

[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:lastIndexPath.row] forKey:@"lastIndexPath"];

您的代码位于UITableViewDelegate方法之外,这就是为什么indexPath和单元格未定义的原因;UITableViewCellAccessoryType是一个C枚举。您可以将其值包装在NSNumber中。将其存储在NSUerDefaults中。然后,当您稍后从NSUserDefaults中提取该数字时,将该值转换为相关的UITableViewCellAccessoryType枚举值。是的,我已经尝试过,但它不起作用,这是我的实现代码:,请您花些时间查看并纠正我的错误,请:而不是:if[[savedState objectForKey:@firstSectionIndex]compare:indexNumber]==使用if[[savedState objectForKey:@firstSectionIndex]isEqual:indexNumber]尝试使用nsorderedname。我不确定在您的代码中是否使用了OrderedName。你到底想做什么?让我解释一下:我有一个包含两个部分的表视图,最初我希望两个部分的第一行都被选中CheckMark,并且我希望每个部分只允许用户选择一个复选标记,然后保留该状态以供长期参考,用于显示以前选定单元格的复选标记