我正在尝试将选中的表格单元格保存并删除未选中的表格单元格到 NSUserdefaults 中,但它似乎忽略了它,因为 for 循环中的 NSLog 永远不会被调用不知道为什么,这段代码有问题吗? (一切都显示正确,单元格得到正确检查和取消检查)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//setting up nsUser
userDefaults = [NSUserDefaults standardUserDefaults];
//current row text
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellName = cell.textLabel.text;
// Check if current row is selected
Boolean isNowChecked = NO;
if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
{
isNowChecked = YES;
}
if(isNowChecked)
{
NSLog(@"UNCHECKING");
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
//takes away highlight from selection
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//retrieve from nsdefaults
NSMutableArray *arrayOfCategories = [userDefaults objectForKey:@"categories"];
NSMutableArray *categoriesSelected;
//remove from nsuserdefaults
//add to nsuserdefaults
for (NSString* o in arrayOfCategories)
{
NSLog(@"%@",o);
if([o isEqualToString:cellName]) {
} else {
[categoriesSelected addObject:o];
}
}
//set for nsdefaults
[userDefaults setObject:categoriesSelected forKey:@"categories"];
[userDefaults synchronize];
}
else
{
NSLog(@"CHECKING");
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
//takes away highlight from selection
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSMutableArray *categoriesSelected;
//add to array
[categoriesSelected addObject:cellName];
//retrieve from nsdefaults
NSMutableArray *arrayOfCategories = [userDefaults objectForKey:@"categories"];
//add to nsuserdefaults
for (NSString* o in arrayOfCategories)
{
NSLog(@"%@",o);
[categoriesSelected addObject:o];
}
//set for nsdefaults
[userDefaults setObject:categoriesSelected forKey:@"categories"];
[userDefaults synchronize];
}
}
最佳答案
改变这一行,否则你有一个未初始化的数组:
NSMutableArray *categoriesSelected;
到:
NSMutableArray *categoriesSelected = [[NSMutableArray alloc] init];
更新:在查看您的代码后,实际上有很多地方需要改进。
- 您应该在
viewDidLoad
中加载一次检查类别列表,并将列表保存在实例变量中。 - 通过查看单元格的附件类型来检查单元格当前是否被选中不是一个好主意。您的数据模型应该告诉您检查了哪些行。
- 您有很多重复代码来切换单元格的选中状态。
- 使用集合而不是数组来跟踪选中的单元格。
尝试以下更改:
在你的类中添加一个 NSMutableSet
实例变量:
NSMutableSet *_checkedCategories;
在viewDidLoad
中,加载集合:
NSArray *checkedCategories = [[NSUserDefault standardUserDefaults] objectForKey:@"categories"];
if (checkedCategories) {
_checkedCategories = [[NSMutableSet alloc] initWithArray:checkedCategories];
} else {
_checkedCategories = [[NSMutableSet alloc] init];
}
现在更新您的 didSelectRow
方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellName = ...; // get the cell name from your data model, not the cell
BOOL isChecked = [_checkedCategories containsObject:cellName];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (isChecked) {
[_checkedCategories removeObject:cellName]; // no longer checked
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
[_checkedCategories addObject:cellName]; // now checked
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[[NSUserDefaults standardUserDefaults] setObject:[_checkedCategories allObjects] forKey:@"categories"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
关于ios - 不确定为什么 NSUserdefaults 不保存结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23208337/