Objective-C,iOS SDK,如何保存UITableView Cell图像复选标记



我正在尝试保存单元格的图像状态,以便当用户退出应用程序或离开视图时,图像仍然保存。我在想NSUserDefaults也许,最好的方法是什么?谢谢

目前我有这个:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell.imageView.image == [UIImage imageNamed:@"checkboxfull.png"]){
    cell.imageView.image = [UIImage imageNamed:@"checkboxblank.png"];
}else if(cell.imageView.image == [UIImage imageNamed:@"checkboxblank.png"])
{
    cell.imageView.image = [UIImage imageNamed:@"checkboxfull.png"];
   // [alert show];
}
}

这实际上取决于您试图保存内容的单元格数量。如果是少数,那么在NSUserDefaults中也没什么大不了的,但如果是更多,也许Core Data会是一个更好的解决方案。

如果要使用NSUserDefaults,可能需要在其中设置BOOL,以指示复选框是打开还是关闭(因为您只有两种状态)。因此:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"someUniqueIdentifier"];

希望这能有所帮助。

我可能会使用NSUserDefaults做一些事情,但我不会单独保存它们。我可能会将每个单元格存储在一个数组中。这将使保存多个单元格变得更加容易。

NSMutableArray *checkState = [[NSArray alloc] init];
if([cell.imageView.image highlighted]) {
   [checkState addObject:[NSNumber numberWithBool:YES]];
} else
   [checkState addObject:[NSNumber numberWithBool:NO]];
Then when you load your table:
NSArray cellState = [NSUserDefaults standardDefaults] objectForKey:@"yourSavedKey"];
[cell.imageView.image setHighlighted:[cellState objectAtIndex:indexPath.row]];

最新更新