我已经写了一个应用程序列出标题,缩略图和一些书的二手价格。这一切都是使用UITableView完成的,数据来自.plist文件。复选框在UITableView单元格
我想要添加的是每个单元格的复选框,如果用户拥有该图书,可以切换到该单元格,然后在下次使用该应用程序时存储该复选框状态。是否可以将值存储回.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文件,因为它是存储在正确的地点。 – rmaddy
所以我需要从捆绑的plist中加载数据,然后创建一个新的plist加载,然后我可以保存到? –