Re: NSTableView row height calculation
Re: NSTableView row height calculation
- Subject: Re: NSTableView row height calculation
- From: Keith Blount <email@hidden>
- Date: Fri, 24 Jun 2005 10:07:03 -0700 (PDT)
Hello,
RowResizableTableView is an excellent subclass, and I
used it before Tiger came along - I optimised the
outline view version quite a lot and added grids and
alternating row colour support, too. You would be more
than welcome to see my optimised code. That said, the
only reason I never got around to sending the
optimised version back to the original writer of the
code (Evan Jones), was that you don't really need it
any more - heightOfRow: is definitely the way to go.
This is the way I do it - this technique is based on
Evan Jones' methods in his subclass, I should add. It
works nicely for NSOutlineView. I've modified my
outline view method to the equivalent table view
method below, so bear in mind that I've only tested
the outline view version:
- (float)tableView:(NSTableView *)
heightOfRow:(int)row
{
// Get column you want - first in this case:
NSTableColumn *tabCol = [[tableView tableColumns]
objectAtIndex:0];
float width = [tabCol width];
NSRect r = NSMakeRect(0,0,width,1000.0);
NSCell *cell = [tabCol dataCellForRow:row];
NSString *content = [[myArray objectAtIndex:row]
target]; // or however you want to get the string
[cell setObjectValue:content];
float height = [cell cellSizeForBounds:r].height;
if (height <= 0) height = 16.0; // Ensure miniumum
height is 16.0
return height;
}
I also implemented this delegate method to ensure
cell-wrapping:
- (void)tableView:(NSOutlineView *)tableView
willDisplayCell:(id)cell forTableColumn:(NSTableColumn
*)tableColumn item:(id)item
{
[cell setWraps:YES];
}
That's problably overkill, though.
Note that using this code, I found that resizing
columns became really ugly and glitchy because the
code above was being called all the time the columns
were resizing, and each time the width would be
different. To get around this, I created an
NSMutableArray *columnWidths ivar in my delegate, and
implemented the following methods in my delegate
(again, converted from my outine view implementation):
- (void)awakeFromNib
{
columnWidths = [[NSMutableArray alloc] init];
int i;
for (i=0;i<[[tableView tableColumns] count];i++)
{
[columnWidths addObject:[NSNumber
numberWithFloat:[[[tableView tableColumns]
objectAtIndex:i] width]]];
}
}
- (void)dealloc
{
[columnWidths release];
[super dealloc];
}
- (void)tableViewColumnDidResize:(NSNotification
*)notification
{
[columnWidths removeAllObjects];
int i;
for (i=0;i<[[tableView tableColumns] count];i++)
{
[columnWidths addObject:[NSNumber
numberWithFloat:[[[tableView tableColumns]
objectAtIndex:i] width]]];
}
[tableView reloadData];
}
If you do this, you will need to replace the line:
float width = [tabCol width];
in tableView:heightOfRow:
with:
float width = [[columnWidths objectAtIndex:0]
floatValue];
Also note that there is no real way to resize the text
field while the user is editing. Evan Jones found a
hack for this in his subclass, but it still had
problems (for instance, if you typed below the bottom
of the scroll view, there was no way to autoscroll to
the place you were typing). I think the best solution
is to just resize once editing is finished, which the
above code should do.
Hope that helps.
All the best,
Keith
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
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