开发者

saving checkmark accessory value in nsuserdefaults and then retreving them

hi i am trying to save state of ticked row in nsuserdefaults.Some how i am getting error :setObject undeclared.

my code is as:

- (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 = [NS开发者_如何学运维UserDefaults standardUserDefaults];
    prefs = [setObject:lastIndexPath forKey:@"lastIndexPath"];

}

also i am trying to fetch row state in viewdidload method(should i fetch here?in view did load).my code is as:

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

and getting errors: indexPath Undeclared and cell undeclared.

i am getting why these errors are coming coz both of them(indexpath and cell) are not in scope,then where to place this code(nsuserdefaults data retriving code).Please guide my. Thanks!


Try replacing this:

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

With this:

[[NSUserDefaults standardUserDefaults] setObject:lastIndexPath forKey:@"lastIndexPath"];

Your error is because you are invoking setObject:forKey: without specifying that your prefs object is calling it. So the compilor is not identifying it as a valid message. You are also trying to assing that messages output to your prefs object, which is not correct.

This said, I am pretty sure you cant store an NSIndexPath inside of a plist directly, you would either have to store as an NSNumber, NSString, or NSData.

This would look like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜