Save selected rows to list in NSuserdefaults

Iam is pretty new to Iphone, and this is my last module in my project. The following code works great for selecting multiple countries using chekmark. Basically, my question is how to save selected countries and show tags again, returning to this view. please help me here.

My method didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

  if([selectedCell accessoryType] == UITableViewCellAccessoryNone) 
  {
    [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
    [selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
  }
  else
  {
    [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
    [selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
  }

  [tableView deselectRowAtIndexPath:indexPath animated:NO];
  [tableView reloadData];
}

CellForRowAtIndexPath method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath 
{
    HolidayAppDelegate *delegatObj = (HolidayAppDelegate *)[UIApplication sharedApplication].delegate;

    static NSString *CellIdentifier = @"CustomCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

    cell.textLabel.text=[delegatObj.allcountryarray objectAtIndex:indexPath.row];

    [cell setAccessoryType:UITableViewCellAccessoryNone];

    for (int i = 0; i < selectedIndexes.count; i++) 
    {
        NSUInteger num = [[selectedIndexes objectAtIndex:i] intValue];
        if (num == indexPath.row) {
            [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
            break;
        }
    }

    return cell;
}

thank

+5
source share
2 answers

If your data is not so big, you can use NSUserDefaults.

To save data:

NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults setObject: selectedIndexes forKey: @ "selection" ];

[ Defaults];

:

NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults objectForKey: @ "selection" ]

0

-

, NSMutableArray array.cout , Country array, default value array, didSelectRowAtIndexPath default value UITableViewCellAccessoryCheckmark.

NSMutableArray NSUserDefaults -

- NSUserDefaults

0

All Articles