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

标签 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];
}

最佳答案

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

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

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

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

关于iOS:如何在 NSUserDefaults 中只保存一个复选标记(用于 UITableView)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13617655/

相关文章:

android - 显示提示以了解位置

objective-c - UITableView 正在填充整个 View

ios - 用户默认值在 Swift 3.0 中给出 nil 值

iphone - AVPlayerItem removeObserver :forKeyPath get error

c# - Mvvmcross:文件插件错误

iOS - 不同 Storyboard 中的 UITableViewCell

swift - 如何在 NSUserdefaults 中快速保存和读取数组数组?

swift - UserDefaults 还是核心数据?

ios - 是否保证在系统更新/设备重启后调用 +load 方法?

ios - 加载主视图或单元格时在 UITableViewCell Swift 中运行代码