Re: NSTableView row height calculation
Re: NSTableView row height calculation
- Subject: Re: NSTableView row height calculation
- From: Steven Spencer <email@hidden>
- Date: Sat, 25 Jun 2005 20:36:27 +0100
Hi Keith,
Thanks for you're reply, it gave a me an idea :
I'm now caching the row heights in a document-level mutable
dictionary, dct.
(An array might be OK too, if it's resized when the number of table
rows changes.)
The row heights are calculated only when required and stored in the
dictionary.
This also avoids the display glitch when resizing the columns.
- (float)tableView:(NSTableView *)tableView heightOfRow:(int)row
{
NSString *key = [NSString stringWithFormat:@"%i", row];
NSNumber *height;
if (!(height = [dct objectForKey:key])) {
//calculate new row height
NSString *str = [[[myController arrangedObjects]
objectAtIndex:row] target];
NSTableColumn *col = [[myTableView tableColumns] objectAtIndex:0];
NSCell *xCell = [col dataCellForRow:row];
[xCell setObjectValue:str];
[xCell setWraps:YES];
height = [NSNumber numberWithFloat:[xCell
cellSizeForBounds:NSMakeRect(0.0, 0.0, [col width], FLT_MAX)].height];
[dct setObject:height forKey:key];
}
return [height floatValue];
}
- (void)tableViewColumnDidResize:(NSNotification *)aNotification
{
[dct removeAllObjects];
[myTableView noteHeightOfRowsWithIndexesChanged:[NSIndexSet
indexSetWithIndexesInRange:NSMakeRange(0, [myTableView numberOfRows]
- 1)]];
}
- (void)tableView:(NSTableView *)tableView didClickTableColumn:
(NSTableColumn *)tableColumn
{
[dct removeAllObjects];
[myTableView noteHeightOfRowsWithIndexesChanged:[NSIndexSet
indexSetWithIndexesInRange:NSMakeRange(0, [myTableView numberOfRows]
- 1)]];
}
When using an array controller, I've found it necessary to add the
following code because array returned by the
arrangedobjects method is empty until the nib has loaded and the
heightOfRow delegate method is called before the nib has loaded.
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
// Add any code here that needs to be executed once the
windowController has loaded the document's window.
[dct removeAllObjects];
[myTableView noteHeightOfRowsWithIndexesChanged:[NSIndexSet
indexSetWithIndexesInRange:NSMakeRange(0, [myTableView numberOfRows]
- 1)]];
}
When using binding with a tableview, is there a way (from the
tableview's perspective) to query the values for the columns and rows ?
(I know how to do this for NSTableDataSource.)
If there is such a way then a tableview sub-class could be written to
provide a general solution for variable row heights.
- Steve Spencer
On 24 Jun 2005, at 18:07, Keith Blount wrote:
Hello,
......
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden