views:

266

answers:

3

Quick one. I'm overlooking something...

I have a grouped table view that is built from arrays in an NSDictionary. Each array is a section of the table. When in editing mode and a user clicks "delete" I call

- (void)removeObject:(MyClass *)myObject

how can i determine which array to send the message [myArray removeObject:myObject]? NSDictionary doesn't have an indexOfObject: method but NSArray does. I suppose I could iterate through each array looking for said object but that doesn't seem right.

Can someone rattle my brain please?!? thx!

A: 

You should know which tableView sent the message (because the method in question is in the delegate for that object) - and therefore you can either create the array in the viewDidLoad method in the tableView delegate, or alternatively figure out which one it is based on the delegate context.

Chaos
A: 

I don't see anything obviously wrong with iterating through the arrays. How often to people delete things, and are the dictionaries really big enough to make this a significant overhead?

As a reference point, NSArray's indexOfObject: method does nothing fancier than iterate though the array looking for a matching object.

grahamparks
A: 

You could implement the standard UITableView protocols and use this method:

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

//check the section according to your array, if(indexPath.section ==....

if(editingStyle == UITableViewCellEditingStyleDelete) {
[arrayFoundBefore removeObjectAtIndex:indexPath.row];
}
}

I think that's the way to do it, not sure though. Can anyone clarify?

natanavra