【问题标题】:delete an item from a NSMutableArray for a specific key从 NSMutableArray 中删除特定键的项目
【发布时间】:2011-08-27 18:44:47
【问题描述】:

我试图弄清楚如何从我用于分区表的 NSMutableArray 中删除一项。这就是我构建数据源的方式:

listOfItems = [[NSMutableArray alloc] init];

NSArray *appleComputers = [NSArray arrayWithObjects:@"iPhone",@"iPod",@"MacBook",@"MacBook Pro",nil];
NSDictionary *appleComputersDict = [NSDictionary dictionaryWithObject:appleComputers forKey:@"Computers"];

NSArray *otherComputers = [NSArray arrayWithObjects:@"HP", @"Dell", @"Windows", nil];
NSDictionary *otherComputersDict = [NSDictionary dictionaryWithObject:otherComputers forKey:@"Computers"];

[listOfItems addObject:appleComputersDict];
[listOfItems addObject:otherComputersDict];

我想弄清楚如何从这个数组中删除,比如“iPod”?我尝试了很多东西,其中包括下面的一个,但没有任何效果。

    [listOfItems removeObjectAtIndex:indexPath.row];

(这里的问题是实际部分没有参考...)

谢谢!

更新:这是我删除行的地方:

- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {


    NSMutableArray * section = [listOfItems objectAtIndex:indexPath.section];    
    [section removeObjectAtIndex:indexPath.row];
[tblSimpleTable reloadData];

} else if (editingStyle == UITableViewCellEditingStyleInsert) {

   // some code...
}

}

【问题讨论】:

    标签: iphone objective-c xcode nsarray


    【解决方案1】:

    不知道您为什么要使用字典...您似乎不需要字典。只是一个数组怎么样?

    listOfItems = [[NSMutableArray alloc] init];
    
    NSMutableArray * appleComputers = [NSMutableArray arrayWithObjects:@"iPhone",@"iPod",@"MacBook",@"MacBook Pro",nil];
    [listOfItems addObject:appleComputers];
    
    NSMutableArray * otherComputers = [NSMutableArray arrayWithObjects:@"HP", @"Dell", @"Windows", nil];
    [listOfItems addObject:otherComputers];
    

    然后

    NSMutableArray * section = [listOfItems objectAtIndex:indexPath.section];    
    [section removeObjectAtIndex:indexPath.row];
    

    即使您确实需要使用字典,这仍然不是什么大问题...只需使用 NSMutableArray 而不是 NSArray 作为您的 appleComputersotherComputers 并执行这个:

    NSDictionary * section = [listOfItems objectAtIndex:indexPath.section];
    NSMutableArray * sectionComputers = [section objectForKey:@"Computers"];    
    [sectionComputers removeObjectAtIndex:indexPath.row];
    

    【讨论】:

    • 这是一个好的开始,但我这个解决方案并没有更新我的原始数据源,而是只创建一个没有删除项目的新数组。
    • 它根本不会创建一个新数组。它会更新您最初添加到列表中的数组。如果您有一个“原始数据源”,而不是您在示例代码中用静态项说明的那个,也许您应该澄清您的问题。
    • 我没有。 listOfItems 是唯一的一个。但是在您的代码中,您正在从 listOfItems 创建“部分”,然后从“部分”中删除一个对象,该对象是 listOfItems 的子集,不再是它的一部分。所以 listOfItems 没有被更新。
    • 我想你很困惑。我不是在listOfItems创建 section,我只是在获取对它的引用。如果您将其用于UITableView,我怀疑您是这样,请确保您在桌子上致电reloadData。此外,将listOfItems 保留在类级别(作为属性/ivar)并在 dealloc 中释放它。
    • +1 我明白你的意思。好吧,它仍然在这里崩溃:[section removeObjectAtIndex:indexPath.row];我已经用删除行的方法更新了我的问题,这就是任何帮助。
    【解决方案2】:

    对于您的示例,您将像这样删除所有“iPod”条目(注意对可变集合的更改):

    listOfItems = [[NSMutableArray alloc] init];
    
    NSMutableArray *appleComputers = [NSMutableArray arrayWithObjects:@"iPhone",@"iPod",@"MacBook",@"MacBook Pro",nil];
    NSMutableDictionary *appleComputersDict = [NSMutableDictionary dictionaryWithObject:appleComputers forKey:@"Computers"];
    
    NSMutableArray *otherComputers = [NSMutableArray arrayWithObjects:@"HP", @"Dell", @"Windows", nil];
    NSMutableDictionary *otherComputersDict = [NSMutableDictionary dictionaryWithObject:otherComputers forKey:@"Computers"];
    
    [listOfItems addObject:appleComputersDict];
    [listOfItems addObject:otherComputersDict];
    
    ...
    
    for (NSMutableDictionary *dict in listOfItems) {
        [dict objectForKey:@"Computers" removeObject:@"iPod"];
    }
    

    【讨论】:

      【解决方案3】:

      你试过 [array removeObject:@"iPod"] 吗?

      【讨论】:

      • 不起作用 - 如果我有多个“iPod”怎么办?我需要使用 indexpath 属性作为参考。不过还是谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多