0

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

@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];
}
4

3 回答 3

1

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

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

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

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

于 2012-11-29T02:14:35.727 回答
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];
}
于 2012-11-29T02:43:56.123 回答
0

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

于 2012-11-29T02:02:00.947 回答