3

我有两个要素:

NSMutableArray* mruItems;
NSArray* mruSearchItems;

我有一个基本UITableViewmruSearchItems,一旦用户滑动并删除一个特定的行,我需要在里面找到该字符串的所有匹配项mruItems并从那里删除它们。

我没有使用足够的 NSMutableArray 并且我的代码由于某种原因给了我错误:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
    //add code here for when you hit delete
    NSInteger i;
    i=0;
    for (id element in self.mruItems) {
        if ([(NSString *)element isEqualToString:[self.mruSearchItems objectAtIndex:indexPath.row]]) {

            [self.mruItems removeObjectAtIndex:i];
        }
        else
           {
            i++;
           }
    }
    [self.searchTableView reloadData];

}    

}

错误:我现在看到某些字符串不在引号之间(UTF8 中的字符串在引号之间)

Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x1a10e0> was mutated while being enumerated.(
    "\U05de\U05e7\U05dc\U05d3\U05ea",
    "\U05de\U05d7\U05e9\U05d1\U05d5\U05df",
    "\U05db\U05d5\U05e0\U05df",
    "\U05d1 ",
    "\U05d1 ",
    "\U05d1 ",
    "\U05d1 ",
    Jack,
    Beans,
    Cigarettes
)'
4

2 回答 2

6

你会得到一个异常,因为你在迭代它的元素时改变了一个容器。

removeObject:完全符合您的要求:删除所有等于参数的对象。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle != UITableViewCellEditingStyleDelete)
        return;

    NSString *searchString = [self.mruSearchItems objectAtIndex:indexPath.row];
    [self.mruItems removeObject:searchString];
    [self.searchTableView reloadData];
}
于 2012-07-11T23:23:24.583 回答
4

您不能在枚举时编辑集合,而是将索引存储起来,然后通过循环遍历索引数组来删除它们。

于 2012-07-11T22:40:18.840 回答