NSTableView sizeToFit is Evil!
NSTableView sizeToFit is Evil!
- Subject: NSTableView sizeToFit is Evil!
- From: Justin Lundy <email@hidden>
- Date: Fri, 8 Aug 2003 19:35:42 -0700
I've been working on this all day and it's driving me MAD!!
Basically, I want a table that, when resized, changes the columns based
on their current widths. You'd think that this would be easy. From the
documentation:
- (void) setAutoresizesAllColumnsToFit: (BOOL) flag
Controls whether the receiver proportionally resizes its columns to fit
when its superview's frame changes. If flag is YES , the difference in
width is distributed among the receiver's table columns; if flag is NO
, only the last column is resized to fit.
Sounds great? But on further investigation, by using
setAutoresizesAllColumnsToFit:YES, the table automatically calls
SizeToFit. Also from the documentation:
- (void) sizeToFit
Changes the width of columns in the receiver so all columns are
visible. All columns are resized to the same size, up to a column's
maximum size. sizeToFit then invokes tile .
setAutoresizesAllColumnsToFit says "proportionally resizes", where
sizeToFit says "resized to the same size". But how can it be both? By
"proportionally," they mean that all the columns are all set to there
minWidth and increased the same amount to fill the frame. No good.
So I set out to subclass NSTableView to make my own sizeToFit that
works properly. Hahaha... Through testing and examination, I found
that almost all that sizeToFit has to do is resize the columns (based
on the current frame) and call tile. I say almost, because it seems to
do something else too. When the columns are already "fitting" (ie. the
horizontal scroll is not showing up and the last column is to the edge
of the vertical scroll) my subclass's sizeToFit seems to works fine.
If I resize the window, the tableColumn (currently set to just resize
one) resizes and all the columns are completely visible. The problem
comes in when the width of the columns is greater (the horizontal
scrollbar is active) or less than (there is a faux column with blank
header next to the vertical scrollbar) the width of the frame, and then
I resize the frame (not the columns) to make it equal. Using the
NSTableView's regular sizeToFit, the columns snap in to place and from
then on are automatically resized. If I use my sizeToFit, though, the
columns get pushed around, but not correctly (usually only going one
direction). If I manually resize the columns so that they "fit," then
my sizeToFit works properly again. This leads me to believe that
manually "fitting" the columns, or using the standard sizeToFit, set
some variable or call some method to lock the columns in to "fitting,"
but for the life of me I have no idea what.
Here is my sizeToFit code from my tableView subclass:
- (void)sizeToFit
{
NSRect aRect = [self frame];
float diff, tot = 0.0;
int i;
NSTableColumn* curCol = [[self tableColumns] objectAtIndex:4];
for (i = 0; i < [[self tableColumns] count]; i++) {
tot = tot + [[[self tableColumns] objectAtIndex:i] width];
}
diff = aRect.size.width - 24.0 - tot; // I believe that 24 is
width of vert. scroll bar
NSLog(@"sizeToFit aRect.width = %f, diff = %f, tot = %f",
aRect.size.width, diff, tot);
[curCol setWidth:([curCol width] + diff)];
[self tile];
// [super sizeToFit];
}
Currently, it only resizes column 4. If you comment out the last two
lines and uncomment [super sizeToFit], you can get the log output while
using the standard sizeToFit.
I am absolutely stumped as to how to fix this. Anyone got any ideas?
Please?
- Justin
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.