1

I have an odd issue cropping up with an NSOutlineView. The view is essentially a list of apps with associated files as children. I populate the view by hand in it's data source and all of that works fine. What I now want to do is have a button to remove an item. In order to do it I implemented a method, removeAppOrFile, like so:

- (IBAction)removeAppOrFile:(id)sender
{
    NSInteger selectedRow = [myView selectedRow];
    if (selectedRow == -1) //ie. nothing's selected
    {
        return;
    }  
    NSTableColumn *col = [myView tableColumnWithIdentifier:@"Column 1"];
    NSCell *cell = [col dataCellForRow:selectedRow];
    NSString *item = [cell stringValue];
    NSLog(@"The row is: %ld\nThe column is: %@\nThe cell is: %@\nThe selected item is: %@",selectedRow, col, cell, item); // For testing purposes
}

myView is an IBOutlet connected to my NSOutlineView. If I select a different row and click the button the value for selectedRow will change properly, but the NSCell object never changes, and what should be it's value (ie. NSString item) always shows that of the last visible item (ie. if there's an item with children as the last item NSString item will be the parent if it's not expanded, or the last child if it is expanded).

The weird thing is that I use basically the same code elsewhere for a doubleAction on an NSOutlineView and it works perfectly. In that case, the code is as below:

- (void)editedAppOrFile:(id)sender 
{
    NSInteger rowNumber = [sender clickedRow];
    NSTableColumn *col = [sender tableColumnWithIdentifier:@"Column 1"];
    NSCell *cell = [col dataCellForRow:rowNumber];
    NSString *item = [cell stringValue];
    NSLog(@"The row is: %ld\nThe column is: %@\nThe cell is: %@\nThe selected item is: %@",selectedRow, col, cell, item); // For testing purposes
}

In this case sender is the outlineView. item & cell change with rowNumber change.

Any idea as to why it's not working in the first example?

Richard
  • 3,316
  • 30
  • 41
VsSoft
  • 288
  • 1
  • 11

1 Answers1

3

There are a few issues with your approach.

  1. You're getting the data cell, not the -preparedCellAtColumn:row:, so you have no guarantees about what its internal object value will be.
  2. You can ask the outline view directly for -itemAtRow:.
  3. If you're trying to remove (in the first case) or edit (the second case), you really only need to modify your data source and then note number of rows changed (first case) or reload data for row (second case).
Richard
  • 3,316
  • 30
  • 41
  • Querying the outline view directly solved the problem. Thanks! – VsSoft Aug 19 '11 at 16:26
  • As for the other comments: As for modifying the data source here's what I'm doing: I have data stored in a dictionary with arrays as values (eg. KEY: App Name VALUES: File1, File2, File3). I'm populating the view with the keys as parents and the FileX as children. When I press the button to delete an item I need to get what item is selected, so I was doing that as above. Then I process the deletion according to if it's a File or App that was selected. I'm probably missing something but I don't see how I could modify the data without getting the item. – VsSoft Aug 19 '11 at 16:36
  • 1
    More to the point, your datasource (controller) was providing model objects to the view, then (through its complicated machinery) asking the view to hand back a property of the model object you just gave it. This is decidedly going against the grain of the MVC design pattern. Asking (as the controller) the outline view for its selection (via -itemAtRow: or NSTableView's selected row indexes) and working with the model object directly is absolutely the way to go. – Joshua Nozzi Aug 19 '11 at 17:13