gpt4 book ai didi

ios - UITableView 单元格中的复选框

转载 作者:行者123 更新时间:2023-11-29 10:47:39 26 4
gpt4 key购买 nike

我写了一个应用程序,列出了一些书的标题、缩略图和二手价格。这一切都是使用 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];

更新:我已经为你创建了一个要点来帮助你将数据加载到 TableView Controller 中 https://gist.github.com/datinc/9075686

关于ios - UITableView 单元格中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21859288/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com