Collection was mutated while being enumerated, UITableView

◇◆丶佛笑我妖孽 提交于 2019-12-22 04:55:29

问题


I have a filter button that presents a UITableView in a popover. I have my categories and an "All" button to denote that no filter is present like in iTunes.

I have a NSMutableDisctionary in my applicationDelegate class that I use to set the checkmarks. When the app starts, only All is selected, everything else is deselected. What I want is then when a row that is not "All" is selected, that row gets selected, and All gets deselected. 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). In my UITableView didSelectRowForIndexPath:, I did this:

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];
}

Two questions. First is, I get this error when I select the first row (All):

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

Where is the enumeration happening? I thought since I only select row 0, I could change the other rows as well, and not just the row 0. I'm not sure what to do about this here.

Second question is is this the way you'd want to update your model class? I wasn't sure if this was considered good MVC. Thanks.


回答1:


The enumeration is the for-loop. You could iterate over a copy of the keys instead to avoid mutating the dictionary while enumerating it:

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


来源:https://stackoverflow.com/questions/6686515/collection-was-mutated-while-being-enumerated-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!