【问题标题】:Collection was mutated while being enumerated, UITableView枚举时集合发生突变,UITableView
【发布时间】:2011-10-04 22:00:34
【问题描述】:

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

我的 applicationDelegate 类中有一个 NSMutableDisctionary,用于设置复选标记。当应用程序启动时,只有全部被选中,其他所有内容都被取消选中。然后,当未选择“全部”的行时,选择该行,选择该行,并取消选择。 Similarly, when All is selected, all the rows with checkmarks do not have checkmarks anymore, and only All is selected with a checkmark (like when the app starts).在我的 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];
}

两个问题。首先,当我选择第一行(全部)时出现此错误:

Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFDictionary: 0x597b3d0> was mutated while being enumerated.

枚举发生在哪里?我想既然我只选择了第 0 行,我也可以更改其他行,而不仅仅是第 0 行。我不知道该怎么做。

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

【问题讨论】:

    标签: iphone objective-c uitableview nsdictionary


    【解决方案1】:

    枚举是for循环。您可以迭代键的副本,以避免在枚举字典时对其进行变异:

    for (NSString *key in [dmgr.CategoryDictionary allKeys]) {
        //...
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-30
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多