Question

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!

Was it helpful?

Solution

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

(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle<br> forRowAtIndexPath:(NSIndexPath *)indexPath {
     <br><br>//check the section according to your array, if(indexPath.section ==....
     <br><br>
    if(editingStyle == UITableViewCellEditingStyleDelete) {
     <br>
    [arrayFoundBefore removeObjectAtIndex:indexPath.row];
    <br> 
    }
    <br>}

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

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top