iOS:如何在NSUserDefaults中只保存一个复选标记(对于UITableView)?

时间:2012-11-29 01:59:16

标签: ios uitableview nsuserdefaults

我对NSUserDefaults有疑问。我试图保存我放在单元格上的复选标记,然后在应用程序崩溃或用户关闭应用程序时检索它等等。我尝试使用this post帖子作为指导,但没有运气,但这是我到目前为止所拥有的。但是,帖子中的代码可以保存,只需要保存一个复选标记,而不是很多。我将如何实现这一目标?

@implementation ClientsViewController

@synthesize clientsInt;        // This is just a variable that helps me do the drill down part of the rootviewcontroller
@synthesize checkedIndexPath;

- (NSString *)getKeyForIndex:(int)index
{
return [NSString stringWithFormat:@"KEY%d",index];
}

- (BOOL) getCheckedForIndex:(int)index
{
if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]] boolValue]==YES)
{
    return YES;
}
else
{
    return NO;
}
}

- (void) checkedCellAtIndex:(int)index
{
BOOL boolChecked = [self getCheckedForIndex:index];

[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]];
[[NSUserDefaults standardUserDefaults] synchronize];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    if([self getCheckedForIndex:indexPath.row]==YES)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

// Configure the cell...
if (clientsInt == 0) {
    cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
}
else if (clientsInt == 1) {
    cell.textLabel.text = [array2 objectAtIndex:indexPath.row];
}
else if (clientsInt == 2) {
    cell.textLabel.text = [array3 objectAtIndex:indexPath.row];
}
else if (clientsInt == 3) {
    cell.textLabel.text = [array4 objectAtIndex:indexPath.row];
}

if([self.checkedIndexPath isEqual:indexPath])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Uncheck the previous checked row
if(self.checkedIndexPath)
{
    UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.checkedIndexPath];
    uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;

[self checkedCellAtIndex:indexPath.row];

if([self getCheckedForIndex:indexPath.row]==YES)
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

3 个答案:

答案 0 :(得分:1)

每次用户检查一行时,也许会尝试这样的事情:

[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:index] forKey:@"kCheckedBoxKey"];

由于每次都将索引保存在同一个键下(@“kCheckedBoxKey”),只会存储一个索引,并且它始终是用户选中的最新索引。下次加载时你需要做的就是询问userDefaults是否可以找到键@“kCheckedBoxKey”的值,如果是,你会通过编程检查存储的索引来响应。

(你当然也希望通过在文件顶部使用#define CHECKED_KEY @"kCheckedBoxKey"来清理它,并使用CHECKED_KEY而不是文字字符串来防止拼写错误。无论如何,重点是确保始终使用相同的密钥保存和恢复。)

答案 1 :(得分:1)

当用户选择或取消选择它们以添加或删除复选标记时,我最近必须在表视图中保存每个单元格的状态。以下是我用来保存到.plist文件的代码片段(如果您需要我提出的整个实现,请告诉我:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *contentForThisRow = [[self list] objectAtIndex:[indexPath row]];
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSString *documentDirectory = [(AppDelegate *)[[UIApplication sharedApplication] delegate] applicationDocumentsDirectory];
    NSString *PlistPath = [documentDirectory stringByAppendingPathComponent:@"Settings.plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:PlistPath];

    NSString *row = [NSString stringWithFormat:@"%d",indexPath.row];

    if([[dict objectForKey:row]isEqualToString:@"0"])
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

    [[cell textLabel] setText:contentForThisRow];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *documentDirectory = [(AppDelegate *)[[UIApplication sharedApplication] delegate] applicationDocumentsDirectory];
    NSString *PlistPath = [documentDirectory stringByAppendingPathComponent:@"Settings.plist"];

    NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile:PlistPath];

    UITableViewCell *cell = [self._tableView cellForRowAtIndexPath:indexPath];

    NSString *row = [NSString stringWithFormat:@"%d",indexPath.row];

    if(cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        NSString *on = @"1";
        [plist setObject:on forKey:row];
        [plist writeToFile:PlistPath atomically:YES];
    }
    else if(cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        NSString *off = @"0";
        [plist setObject:off forKey:row];
        [plist writeToFile:PlistPath atomically:YES];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

答案 2 :(得分:0)

您只需要保存所选行的索引