views:

105

answers:

1

I'm using a Table View in a Cocoa application. I have set the double click action to do the following method when it occurs:

- (void)doubleClickInTable:(id)sender {
      int rowIndex = [sender selectedRow];
      if (rowIndex != -1) {
        [userEditController setData:[[self users] objectAtIndex:rowIndex]];
        [self showUserEditPanel];
    }
}

As you can see, the EditController receives the object that is being edited. This object is the object that is located at rowIndex of the source array. This works very well most of the time, but once I started testing the sorts it is setting the wrong object. This is because the index of the clicked row in the table is different then the source array due to the sort moving rows around.

How do I fix this issue?

+2  A: 

You could create a sorted array using the same sort descriptors, and retrieve the object at rowIndex in that. The other way, if you're using an array controller, would be to retrieve the object at that index in the controller's arrangedObjects array, which is already sorted (hence its name).

Peter Hosey
I recommend using NSArrayController as Peter mentions.
sbooth