【问题标题】:save selected cheklist rows in NSuserdefaults在 NSuserdefaults 中保存选定的检查表行
【发布时间】:2011-06-19 09:01:42
【问题描述】:

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

我的 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;
}

谢谢

【问题讨论】:

    标签: ios nsuserdefaults


    【解决方案1】:

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

    保存数据:

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

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

    [用户默认同步];

    获取数据:

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    [userDefaults objectForKey:@"selection"]

    【讨论】:

      【解决方案2】:

      有很多方法可以解决它--

      根据我的说法,您必须创建一个 NSMutableArrayarray.cout 与您的 Country array 相同,最初您将 default value 存储在您的 array 中, 在didSelectRowAtIndexPath 中,您必须根据UITableViewCellAccessoryCheckmark 更改存储的default value

      然后你必须将NSMutableArray 存储在NSUserDefaults -

      Link - FOR SAVE ARRAY IN NSUserDefaults

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-14
        • 1970-01-01
        • 2018-10-08
        • 1970-01-01
        • 2011-10-02
        • 1970-01-01
        相关资源
        最近更新 更多