将选中标记的行保存到appsettings中。 EDITED

时间:2011-04-18 18:05:47

标签: iphone cocoa-touch uitableview

编辑:我收到了保存,现在它无法正常工作。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:countries  forKey:@"country"];
[defaults synchronize]; 

保存。

但我想我必须从appsettings加载数组。如何?

现在有了这个:

countries = [[NSArray alloc] initWithObjects:@"All",@"Austria",@"Belarus",@"Canada",@"Czech Republic",@"Denmark",@"Germany",@"Finland",@"France",@"Latvia",@"Norway",@"Russia",@"Slovakia",@"Slovenia",@"Switzerland",@"Sweden", @"USA", nil];

1 个答案:

答案 0 :(得分:0)

使用 NSUserDefault 同步方法。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        int newRow = [indexPath row];
        int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;

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

            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: 
                                        lastIndexPath]; 
            oldCell.accessoryType = UITableViewCellAccessoryNone;
            lastIndexPath = indexPath;
        }
        NSUInteger row = [indexPath row];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        //Suppose you have used **myArrayUsedInTableView**   Array to construct TableView 
        ..................................................................................
        ............./*Do changes here in myArrayUsedInTableView  as per your requirement*/
        ....................................................................................
        //Now store your array myArrayUsedInTableView in NSUserDefault
        [defaults setObject:myArrayUsedInTableView  forKey:@"StoredArray"];
        [defaults synchronize]; 
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
  }