Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
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?
 
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)]];
}

Since NSTableView is a subclass of NSView could you use setNeedsDisplay?

[tableView setNeedsDisplay:YES];

Good suggestion. I have to try it. This might be more efficient.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.