2011-08-27 62 views
2

我想弄清楚如何从这个NSMutableArray中删除一个项目用于分区表格。这是我如何建立数据源:从特定键的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]; 

(这里的问题是,没有为实际的部分没有提及...)

谢谢!

UPDATE:这是我删除该行:

- (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... 
} 

}

回答

2

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

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而不是NSArrayappleComputersotherComputers和做到这一点:

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

这是一个很好的开始,但是这个解决方案并没有更新我的原始数据源,而是它只创建了一个没有删除项目的新数组。 – TommyG

+0

它根本不创建新的数组。它会更新您最初添加到列表中的数组。如果您有一个“原始数据源”,而不是您在示例代码中使用静态项目说明的“原始数据源”,那么您可能应该澄清一下您的问题。 – Steve

+0

我不知道。 listOfItems是唯一的一个。但是在你的代码中,你是从listOfItems中创建“section”,然后从“section”中删除一个对象,它是listOfItems的一个子集,而不再是它的一部分。所以listOfItems没有被更新。 – TommyG

0

你试过[数组的removeObject:@的 “iPod”]?

+0

不工作 - 如果我有多个“iPod”?我将需要使用indexpath属性作为参考。不过谢谢。 – TommyG

1

对于您的示例,您将删除所有'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"]; 
}