UITableView单元格中的复选框 [英] Checkbox in UITableView Cell

查看:90
本文介绍了UITableView单元格中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个应用程序,其中列出了一些书的标题,缩略图和二手价格.所有操作都使用UITableView完成,数据取自.plist文件.

I've written an app which list the title, a thumbnail and the second-hand price of some books. It's all done using a UITableView and the data is taken from a .plist file.

我要添加的是每个单元格的复选框,如果用户是否有书,可以切换该复选框,然后在下次使用该应用程序时存储该复选框状态.是否可以将值存储回.plist文件,还是有另一种方法?

What I want to add is a checkbox to each cell which can be toggled if the user has the book or not and then store that checkbox state for the next time they use the app. Is it possible to store a value back to the .plist file or is there another way to do it?

这是我到目前为止为单元格提供的代码:

Here's the code I have so far for the cell:

@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来预先填充数据库.

There are lots of alternatives to storing the data in a plist. Core Data would be my choice. Learning how to use core data is worth the time investment. You could still use the plist to pre populate your database.

但是,回答您的问题是,您可以将数据存储在plist中.假设初始数据存储在分发包中,并且是一个字典数组,下面是一些示例代码.

However to answer your question yes you can store the data in a plist. Assuming the initial data is stored in the bundle and is an array of dictionaries here is some example code.

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

Update: I have created a gist for you to help you load the data into a table view controller https://gist.github.com/datinc/9075686

这篇关于UITableView单元格中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆