收集在被枚举时变异,UITableView

Cry*_*tal 7 iphone objective-c nsdictionary uitableview

我有一个过滤器按钮,在弹出窗口中显示UITableView.我有我的类别和"全部"按钮,表示没有像iTunes中那样存在过滤器.

我的applicationDelegate类中有一个NSMutableDisctionary,用于设置复选标记.当应用程序启动时,仅选择All,取消选择其他所有内容.我想要的是当选择不是"All"的行时,该行被选中,All被取消选择.类似地,当选择All时,带有复选标记的所有行都不再具有复选标记,并且仅选中All并带有复选标记(例如应用程序启动时).在我的UITableView didSelectRowForIndexPath:中,我这样做了:

MyAppAppDelegate *dmgr = (MyAppAppDelegate *)[UIApplication sharedApplication].delegate;
NSUInteger row = [indexPath row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

// All selected
if (row == 0) {
    for (NSString *key in dmgr.CategoryDictionary) {
        [dmgr.CategoryDictionary setObject:[NSNumber numberWithBool:NO] forKey:key];
    }
    [dmgr.CategoryDictionary setObject:[NSNumber numberWithBool:YES] forKey:@"All"];                
}

else {

    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    NSString *key = [_categoriesArray objectAtIndex:row];
    BOOL valueAtKey = [[dmgr.CategoryDictionary valueForKey:key] boolValue];
    valueAtKey = !valueAtKey;       
    [dmgr.CategoryDictionary setObject:[NSNumber numberWithBool:valueAtKey] forKey:key];
}
Run Code Online (Sandbox Code Playgroud)

两个问题.首先,当我选择第一行(全部)时,我收到此错误:

Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFDictionary: 0x597b3d0> was mutated while being enumerated.
Run Code Online (Sandbox Code Playgroud)

枚举在哪里发生?我想,因为我只选择第0行,我也可以更改其他行,而不只是第0行.我不知道该怎么办.

第二个问题是,您想要更新模型类的方式是什么?我不确定这是否被认为是好的MVC.谢谢.

omz*_*omz 20

枚举是for循环.您可以迭代密钥的副本,以避免在枚举时改变字典:

for (NSString *key in [dmgr.CategoryDictionary allKeys]) {
    //...
}
Run Code Online (Sandbox Code Playgroud)