【发布时间】:2014-03-22 18:42:32
【问题描述】:
背景:我想创建一个应用程序,其中列出了一些人可能正在收集的书籍,他们可以将其用作清单。我想在完全启动测试之前让测试正常工作,并且到目前为止我已经成功地完成了以下工作。
使用 plist、UITableView 和 UITableCell 我可以填充表格。单元格由缩略图、书名和二手价格组成。还有一个“复选框”按钮,用户可以切换是否已经拿到这本书。我想要做的是在退出时保存复选框切换的状态。也许将一个值存储到 .plist 中(我已将其复制到 Documents 目录中,以便在必要时进行编辑)?还是有其他方法?我可以使用 NSUserDefaults 来做到这一点,但我怎样才能让它与每个单元格(书)相关联?
正在运行的测试应用程序的屏幕截图(暂时请原谅基本图形):
这是单元格布局和相关的 .h 文件:
这是主 ViewController .m 文件的代码:
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Find out the path of books_star.plist
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Load the file content and read the data into arrays
NSString *myListPath = [documentsDirectory stringByAppendingPathComponent:@"books_star.plist"];
tableData = [[NSArray alloc]initWithContentsOfFile:myListPath];
NSLog(@"%@",tableData);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *books_starTableIdentifier = @"booksCell";
booksCell *cell = (booksCell *)[tableView dequeueReusableCellWithIdentifier:books_starTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"booksCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.titleLabel.text = [[tableData objectAtIndex:indexPath.row]objectForKey:@"title"];
cell.thumbnailImageView.image = [UIImage imageNamed: [[tableData objectAtIndex:indexPath.row]objectForKey:@"thumbnail"]];
cell.priceLabel.text = [[tableData objectAtIndex:indexPath.row]objectForKey:@"price"];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 96;
}
@end
这是 Cell .m 文件的代码:
#import "booksCell.h"
@implementation booksCell
@synthesize titleLabel = _titleLabel;
@synthesize priceLabel = _priceLabel;
@synthesize thumbnailImageView = _thumbnailImageView;
@synthesize checkBoxButton;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {}
return self;}
- (void) awakeFromNib {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
checked = [defaults boolForKey:@"boxIsChecked"];
[self checkTheBox];
}
- (IBAction)checkButton:(id)sender {
if (!checked) {
[checkBoxButton setImage:[UIImage imageNamed:@"check_on.png"] forState:UIControlStateNormal];
checked = YES;
NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile:@"books_star.plist"];
[plist setObject:[NSNumber numberWithBool:YES] forKey:@"check"];
[plist writeToFile:@"books_star.plist" atomically:YES];
}
else if (checked) {
[checkBoxButton setImage:[UIImage imageNamed:@"check_off.png"] forState:UIControlStateNormal];
checked = NO;
}
}
- (void) checkTheBox {
if (!checked) {
[checkBoxButton setImage:[UIImage imageNamed:@"check_off.png"] forState:UIControlStateNormal];
}
else if (checked) {
[checkBoxButton setImage:[UIImage imageNamed:@"checkBox_on.png"] forState:UIControlStateNormal];}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
最后是 .plist 文件:
【问题讨论】:
-
为什么您不接受答案或继续上一个关于该问题的帖子:stackoverflow.com/questions/21909051/…
-
嘿。我在聊天中发了几次,但没有得到任何回复。 :(
标签: ios uitableview plist selection