UITableView Cell中的复选框

我写了一个应用程序,列出标题,缩略图和一些书籍的二手价格。 这一切都是使用UITableView完成的,数据来自.plist文件。

我要添加的是每个单元格的复选框,如果用户有书,可以切换,然后在下次使用应用程序时存储该复选框状态。 是否可以将值存储回.plist文件或是否有其他方法可以执行此操作?

这是我到目前为止为单元格编写的代码:

@implementation ffbooksCell @synthesize ffbooksLabel = _ffbooksLabel; @synthesize priceLabel = _priceLabel; @synthesize thumbnailImageView = _thumbnailImageView; - (void) viewDidLoad { NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString* path = [documentsDirectory stringByAppendingPathComponent:@"ffbooks.plist"]; NSMutableArray* checkboxSelected = nil; // Attempt to load saved data from the documents directory if ([[NSFileManager defaultManager] fileExistsAtPath:path]){ checkboxSelected = [NSMutableArray arrayWithContentsOfFile:path]; }else{ // No saved data in documents directory so we need to load the data from the bundle NSString* bundlePath = [[NSBundle mainBundle] pathForResource:@"ffbooks" ofType:@"plist"]; checkboxSelected = [NSMutableArray arrayWithContentsOfFile:bundlePath]; } //... // Toggle check box for book 0 if ([checkboxSelected[0][@"checkbox"] boolValue]){ checkboxSelected[0][@"checkbox"] = @NO; }else{ checkboxSelected[0][@"checkbox"] = @YES; } // Note: @"checkbox" does not need to be in the original plist //... [checkboxSelected writeToFile:path atomically:YES]; if (checkboxSelected == 0){ [checkboxButton setSelected:NO]; } else { [checkboxButton setSelected:YES]; } } - (IBAction)checkboxButton:(id)sender{ if (checkboxSelected == 0){ [checkboxButton setSelected:YES]; checkboxSelected = 1; } else { [checkboxButton setSelected:NO]; checkboxSelected = 0; } } - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end 

将数据存储在plist中有很多替代方法。 核心数据将是我的选择。 学习如何使用核心数据值得投入时间。 您仍然可以使用plist预填充数据库。

但是,要回答您的问题,您可以将数据存储在plist中。 假设初始数据存储在bundle中并且是一个字典数组,这里是一些示例代码。

 NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString* path = [documentsDirectory stringByAppendingPathComponent:@"books.plist"]; NSMutableArray* info = nil; // Attempt to load saved data from the documents directory if ([[NSFileManager defaultManager] fileExistsAtPath:path]){ info = [NSMutableArray arrayWithContentsOfFile:path]; }else{ // No saved data in documents directory so we need to load the data from the bundle NSString* bundlePath = [[NSBundle mainBundle] pathForResource:@"books" ofType:@"plist"]; info = [NSMutableArray arrayWithContentsOfFile:bundlePath]; } //... // Toggle check box for book 0 if ([info[0][@"checkbox"] boolValue]){ info[0][@"checkbox"] = @NO; }else{ info[0][@"checkbox"] = @YES; } // Note: @"checkbox" does not need to be in the original plist [info writeToFile:path atomically:YES]; 

更新:我已经为您创建了一个要点,以帮助您将数据加载到表视图控制器https://gist.github.com/datinc/9075686

您可以使用NSUserDefaults存储少量数据(主要是设置……)

请查看本教程