2014-02-18 80 views
1

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

是的,你可以只要更新plist文件,因为它是存储在正确的地点。 – rmaddy

+0

所以我需要从捆绑的plist中加载数据,然后创建一个新的plist加载,然后我可以保存到? –

回答

1

在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

+0

好的。所以我的plist文件被称为ffbooks,并有三个根数组(BookName,Thumbnail和Price)。我是否需要添加第四个数组来存储复选框值(0或1)以及如何回忆这些数据? –

+0

将代码添加到初始帖子。 –

+0

我认为你应该将回答标记为答案。它根据你询问的是从plist加载/保存数据。我不确定你现在要求什么,然后为你写控制器。 – datinc

0

您可以使用NSUserDefaults存储数据量小(大多喜欢设置...)

只是检查this tutorial

+0

谢谢。我可以使用NSUserDefaults,并且已经完成了许多应用程序,但不知道如何将它集成到从.plist文件填充的TableView单元中。 –

+0

对不起,你的问题一见钟情。 你仍然可以设置成'NSUserDefaults'一个简单类型的NSDictionary('NSNumber','NSString')。所以你可以用它来存储你的数据。 您也可以使用.plist文件,但它会要求您执行一些文件管理,这种文件管理在最后会更重(但会带来更多选项)。这真的取决于你打算做什么,数据量等。 – AncAinu