delete an item from a NSMutableArray for a specific key
I am trying to figure out how to delete one item from this NSMutableArray that I am using for a sectioned table. This is how I build the data source:
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];
I am trying to figure out how to delete, say "iPod" from this array? I have tried many things, among them the one below, but nothing worked.
[listOfItems removeObjectAtIndex:indexPath.row];
(problem here is that there is no reference for the actual section...)
Thanks!
开发者_运维问答UPDATE: This is where I delete the 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...
}
}
Not sure why you're using a dictionary... doesn't seem like you need one. How about just an array of arrays?
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];
then
NSMutableArray * section = [listOfItems objectAtIndex:indexPath.section];
[section removeObjectAtIndex:indexPath.row];
Even if you do need to use the dictionaries, it's still not a big deal... just use NSMutableArray
instead of NSArray
for your appleComputers
or otherComputers
and do this:
NSDictionary * section = [listOfItems objectAtIndex:indexPath.section];
NSMutableArray * sectionComputers = [section objectForKey:@"Computers"];
[sectionComputers removeObjectAtIndex:indexPath.row];
For your example, you'd remove all 'iPod' entries like this (note changes to mutable collections):
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"];
}
Have you tried [array removeObject:@"iPod"]?
精彩评论