ios - 在 NSuserdefaults 中保存选定的 list 行

标签 ios nsuserdefaults

我是 Iphone 的新手,这是我项目中的最后一个模块。以下代码非常适合使用 chekmark 选择多个国家/地区。 基本上我的问题是如何保存这些选定的国家并在返回此 View 时再次显示复选标记。 请在这里帮助我..

我的 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 方法

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

谢谢

最佳答案

如果你的数据不是很大,你可以使用 NSUserDefaults。

保存数据:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

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

[userDefaults synchronize];

获取数据:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults objectForKey:@"selection"]

关于ios - 在 NSuserdefaults 中保存选定的 list 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6401494/

相关文章:

ios - 为什么不应该将 UIWebView 放在 UIScrollView 中?

ios - iOS 中缓慢的默认访问崩溃

iphone - managedObjectContext 似乎删除了我的 NSUserDefault

ios - 尝试构建到设备后无法启动“<AppName>”

ios - 读取 NSUserDefaults 与变量的速度

iphone - 究竟是决定使用NSUserDefaults还是CoreData的“大量数据”是什么?

swift - 默认值太复杂,无法使用基本的 '+' 运算符进行编译

iPhone 编程 - 在 View Controller 之间传递 NSMutableArray

ios - 用户触摸后线路旋转

ios - 快速文本标签每周更新