NSActionCell in NSTableView

Discussion in 'Mac Programming' started by Stratoukos, Jun 11, 2010.

  1. Stratoukos macrumors member

    Joined:
    Jul 15, 2008
    #1
    I have a NSTableView in which I want, among other things, I want to display a NSActionCell with an image.

    Inside tableView: objectValueForTableColumn:row: I have
    Code:
    ...
    else if ([[tableColumn identifier] isEqualToString:@"icon"]) {
    	NSActionCell *cell = [[NSActionCell alloc] init];
    	[cell setImage:[NSImage imageNamed:NSImageNameRevealFreestandingTemplate]];
    	return cell;
    } 
    ...
    
    When I run this I get "NSImageCell's object value must be an NSImage." and the app hangs. The same thing happens If I use a NSImageCell. If I return just a NSImage the image displays, but then I can't have any actions on it.

    Any help would be greatly appreciated.
     
  2. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #2
    What happens if you just return the image as the datasource return value?

    Note that a table uses a single cell for all the rows in a given column, so your method would be leaking very badly. You want to define the cell once in the column for that table (not in the datasource method) and just return the image.
     
  3. Stratoukos thread starter macrumors member

    Joined:
    Jul 15, 2008
    #3
    If I return just the image it displays normally inside the table view. Also [image isKindOfClass:[NSImage class]] returns true, so I'm sure it's an NSImage.

    I could do that, but I want something to happen when a user clicks on the image, so I went with NSActionCell. I don't know if this is the "right" way to do it, but I can't think of something else.

    You are, of course, right about the leak.
     
  4. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #4
    If you want something to happen when the user clicks on the cell, then you need to put the cell you want into that column. You could do it by creating a subclass (I have done that) or you could set the action and target for the cell itself (things you can do in Interface Builder). You might also investigate the delegate methods NSTableView has to offer, you might be able to work out a response from one of those. The other alternative is to just subclass NSTableView itself. Sometimes that is the best way to go.
     

Share This Page