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?
then
Even if you do need to use the dictionaries, it’s still not a big deal… just use
NSMutableArrayinstead ofNSArrayfor yourappleComputersorotherComputersand do this: