开发者

save selected cheklist rows in NSuserdefaults

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


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"]


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜