5

Iam pretty new to Iphone and this is my last module in my project..The following code working perfectly for multiple country selection with chekmark. Basically my question is how to save those selected countries and show the check marks again when come back to this view. please help me here..

My didSelectRowAtIndexPath method

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

My 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;
}

Thanks

tilo
  • 14,009
  • 6
  • 68
  • 85

2 Answers2

0

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

To save data:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

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

[userDefaults synchronize];

To get data:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults objectForKey:@"selection"]

Community
  • 1
  • 1
David V
  • 2,134
  • 1
  • 16
  • 22
0

There are so many way to solve it --

According to me you have to create a NSMutableArray and array.cout same as your Country array and initially you store the default value in your array , in didSelectRowAtIndexPath you have to change the stored default value according to UITableViewCellAccessoryCheckmark .

And after that you have to store the NSMutableArray in the NSUserDefaults -

Link - FOR SAVE ARRAY IN NSUserDefaults

Abhishek Mishra
  • 1,625
  • 16
  • 32