【发布时间】:2014-12-21 16:27:11
【问题描述】:
这就是我当前保存单元格复选标记状态的方式(有效):
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {\
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[defaults setBool:YES forKey:cell.textLabel.text];
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
[defaults setBool:NO forKey:cell.textLabel.text];
}
[defaults synchronize];
}
这就是我尝试检索它们的方式:
-(void)viewWillAppear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
for (int section = 0; section < [self.tableView numberOfSections]; section++) {
for (int row = 0; row < [self.tableView numberOfRowsInSection:section]; row++) {
NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:cellPath];
if (![defaults valueForKey:cell.textLabel.text]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
}
NSUser 默认保存正确(我知道这是因为 NSLog),但是当我退出并返回表格视图时,没有单元格有复选标记。
如果我换行:
if (![defaults valueForKey:cell.textLabel.text]) {
到
if ([defaults valueForKey:cell.textLabel.text]) {
表格视图重新出现,所有单元格都带有复选标记。
谢谢
【问题讨论】:
标签: ios xcode uitableview cell