我正在尝试保存已检查并将未经检查的tablecells删除到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];
}
}
答案 0 :(得分:4)
更改此行,否则您有一个未初始化的数组:
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];
}