1

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 = [NSUserDefaults 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!

Joe
  • 56,979
  • 9
  • 128
  • 135
maddy
  • 4,001
  • 8
  • 42
  • 65
  • 1
    You have your code outside of a `UITableViewDelegate` method that is why indexPath and cell is undefined. – Joe Apr 07 '11 at 13:47

1 Answers1

0

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"];
Sabobin
  • 4,256
  • 4
  • 27
  • 33
  • Thanks,but how about retrieving the same to set cell accessory type ;) – Eshwar Chaitanya May 15 '13 at 07:10
  • UITableViewCellAccessoryType is a C enum. You could wrap its value in an NSNumber. Store that in NSUerDefaults. Then later when you pull that number out of NSUserDefaults, translate the value to the relevant UITableViewCellAccessoryType enum value. – Sabobin May 15 '13 at 10:15
  • Yeah I have tried,but it is not working,here is my implementation code: http://pastebin.com/MmC6EcYu ,can you please take some time to view and rectify my mistake,please :) – Eshwar Chaitanya May 15 '13 at 10:30
  • Instead of: if([[savedState objectForKey:@"firstSectionIndex"] compare: indexNumber] == NSOrderedSame) try using if([[savedState objectForKey:@"firstSectionIndex"] isEqual: indexNumber]). Im not sure about the use of NSOrderedSame throughout your code. What exactly are you trying to do? – Sabobin May 15 '13 at 11:40
  • Let me explain you:I have a table view with 2 sections,initially I want the first row of both the sections to be checked(checkmark) and I want to allow only one check mark per section for user selection of cell,then preserve that state for long term reference,use the same for displaying check marks for previously selected cell – Eshwar Chaitanya May 15 '13 at 12:08
  • So save section and row of that cell in NSUserDefaults. Then later when you need the values, pull the section and row out. Use those values to decide which cells need to be checked, and which cells dont need to be checked. – Sabobin May 15 '13 at 12:45
  • Can you please modify my code in paste bin accordingly,I have tried several ways and no use :( – Eshwar Chaitanya May 15 '13 at 13:31
  • At last I could initially set check marks for 1st row in both sections,then save the check mark state for user selection of cells and display the same,but the only problem is:"When I select bottom row of a section",dismiss the view and come back,select the up rows,the bottom cell is still showing check mark,let's say a section with 3 rows,selected 3rd row,dismissed the view and came back,selected 2nd or 1st,the 3rd row as well as the selected row is showing check mark,wonder why,can you please kindly check my code,please help me get over this minute problem: http://pastebin.com/U7ySKeys – Eshwar Chaitanya May 16 '13 at 11:25
  • in cellForRowAtIndexPath: when you get the cell. Make sure you reset the cells accessory type. cell.accessoryType = UITableViewCellAccessoryNone. UITableViewCells are reused by the table view, meaning that unless you reset its properties manually, they will stay the same as they were previously. – Sabobin May 16 '13 at 11:47
  • I am sorry I have reset the cell accessory type to none in cellForRowAtIndexPath method,i.e. just before switch case in my code,but still the same problem persists :( – Eshwar Chaitanya May 16 '13 at 12:30