How to preserve the index path value in the table

2019-06-05 01:29发布

问题:

I have created a table view and displayed the datas. When i click the data in the table view, i put the accessory mark (using UITableViewCellAccessoryCheckmark, Like, Check Mark). Now i want to preserve the state of index position. Because when i go to the another class and then come back to the table view, the previous state(accessory mark) will be displayed. SO how can i preserve the state, which means store or save the indexpath value.(Without using AppDelegate method)so How can i achieve this?

Here my sample code is,

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 if (newRow != oldRow)
{
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                indexPath];
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
                                checkedData];
    oldCell.accessoryType = UITableViewCellAccessoryNone;

    checkedData = indexPath;

}
if (newRow == oldRow) {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    } else {

        // cell.accessoryType = UITableViewCellAccessoryNone;
    }
    checkedData = indexPath;
}
 }

When back to the table view class, the previous state should be preserved. so how can i access that?

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(checkedData == indexPath) // Doesn't works
      {

    cell.accessoryType = UITableViewCellAccessoryCheckmark;
     }

Please help me out.

Thanks

回答1:

Variables are unavailable once you reach the end of scope (I don't know if they're nil or released, I just know you can't use them).

What you want to do is A) save that value to persistent storage or B) make a persistent variable.

A) Set the object to storage (In this case NSUserDefaults, because it's dead easy:

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

Then, to check the object the way you wanted to, you could do this:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if([[prefs objectForKey:@"checkedData"] compare: indexPath]== NSOrderedSame){
cell.AccessoryType = UITableViewCellAccessoryCheckMark;
}

B) Save the object as a persistent variable:

In your .h:

@interface ... : ...{

}

@property(nonatomic, retain) NSIndexPath *checkedData;

@end

In the .m:

@implementation 
@synthesize checkedData;
@end

Now, to set this variable:

self.checkedData = indexPath;

To check it:

if([self.checkedData compare:indexPath] == NSOrderedSame){
cell.accessoryType = UITableViewCellAccessoryTypeCheckMark;
}

There really isn't going to be a whole lot of difference, it's up to you to decide what you want. If you use NSUserDefaults though, keep in mind that the data persists across launches. If you don't want that, but you want to use it you have to empty the object when you close the app: [prefs removeObjectForKey:@"checkedData"];

Happy coding,

Zane