setting height of custom view in NSTableView

Discussion in 'Mac Programming' started by MrFusion, Aug 25, 2011.

  1. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #1
    Since 10.7 NSTableView supports custom views.
    Just drag a custom view to a table column, update the subclass of the view and of you go.

    However, if you use a delegate for your NSTableView and your class has an outlet to the NSTableView, then don't forget to implement
    Code:
    -(NSView *) tableView:(NSTableView *)tableView 
      viewForTableColumn:(NSTableColumn *)tableColumn 
      row:(NSInteger)row;
    
    in addition to
    Code:
    -(id) tableView:(NSTableView *)aTableView 
      objectValueForTableColumn:(NSTableColumn *)aTableColumn  
      row:(NSInteger)rowIndex;
    
    That should save you hours of debugging and 10 minutes of reading the documentation.

    The problem I am having is this:
    I added a custom view to a NSTableView and its content, layout and required height are based on the objectValue. Calculating the required height is quite complex and the required code is placed within the custom view subclass rather than within the NSTableView delegate/datasource.

    The data is correctly loaded and by calling
    Code:
    -(void) noteHeightOfRowsWithIndexesChanged:(NSIndexSet *)indexSet:
    
    the height is correctly adjusted because
    Code:
    -(CGFloat) tableView:(NSTableView *)tableView 
      heightOfRow:(NSInteger)row
    
    is called by the NSTableView.

    When I change the datasource or the table contents, etc. I know I have to call reloadData and/or noteHeightOfRowsWithIndexesChanged to update the table and adjust the row heights.

    However, when a NSTableView is loaded from a xib file, cocoa first sets the row heights and then loads the data. But after the data has been loaded for the first time, the row heights need to be updated. How can I force a reload?

    I tried adding noteHeightOfRowsWithIndexesChanged to:
    Code:
    -(void) awakeFromNib;
    -(void) windowControllerDidLoadNib:(NSWindowController *)aController;
    
    but that didn't work.

    Any idea how to do this?
     
  2. cbougher macrumors member

    Joined:
    Oct 19, 2006
    Location:
    Atlanta, GA
    #2
    Since NSTableView is a subclass of NSView could you use setNeedsDisplay?

    [tableView setNeedsDisplay:YES];
     
  3. MrFusion thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #3
    I eventually came up with this:
    Code:
    -(void) tableView:(NSTableView *)tableView 
    	didAddRowView:(NSTableRowView *)rowView 
    		   forRow:(NSInteger)row
    {
    	//force update of row height
    	[tableView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(row, 1)]];
    }
    
    
    Good suggestion. I have to try it. This might be more efficient.
     

Share This Page