UITableView Cell中的复选框

时间:2014-02-18 16:13:31

标签: ios uitableview plist

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

2 个答案:

答案 0 :(得分:2)

有很多替代方法可以将数据存储在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

答案 1 :(得分:0)

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

只需查看this tutorial

即可