我写了一个应用程序,其中列出了一些书的标题,缩略图和二手价格。全部使用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中。假设初始数据存储在包中,并且是一个字典数组,这里是一些示例代码。
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
关于ios - UITableView单元格中的复选框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21859288/