Skip navigation.
 
mlRe: Yann Disser Resizing NSTableView columns
FROM : Corbin Dunn
DATE : Mon May 12 19:12:03 2008

An easier algorithm to do this on Leopard:

- (CGFloat)mySizeToFitWidthOfColumn:(NSInteger)column row:
(NSInteger)row {
    NSCell *cell = [self preparedCellAtColumn:column row:row];
    return [cell cellSize].width + 1;
}

..

// for each table column:
        for (NSInteger i = 0; i < [self numberOfRows]; i++) {
            NSInteger cellWidth = [self 
mySizeToFitWidthOfColumn:column row:i];
            // Optional: constrain it to the column min/max of the 
column, or the table itself, if there is no column (which...is strange 
for autoresize, but oh well)
            sizeToFitWidth = MAX(sizeToFitWidth, cellWidth);
        }
...set the width on the table column

-corbin


On May 10, 2008, at 6:44 PM, Peter Hudson wrote:
> sizeToFit  simply sets the width of a column to the width of the 
> header cell - and I suspect that you
> want to set the width of each column to suit the widest  cell  of 
> data in that column.
>
> Here is the heuristic to achieve that:
>
> ( Assuming that you have not changed the font for any of the 
> cells ... )
>
>
> [A]
> get a table column  -  get the dataCell  for the column  -  send 
> it the  font  message.
> Assume we assign it to a variable  tableFont
>
> [B]
> Create an attributes dictionary with tableFont  like this ...
>
> NSMutableDictionary *attributes = [NSMutableDictionary  dictionary];
> [attributes  setObject:tableFont  forKey:NSFontAttributeName];
>
>
> [C]
> Then, by using the table data source, you use this attributes 
> dictionary to check
> the written size of all the data - column by column.
> i.e. take each column and iterating over the data for each row in 
> that column,
> hence deducing the max width of the column....
>
>     NSSize  writtenSize = [dataString  sizeWithAttributes:attributes];
>
> [D]
> Having collected the widths of all the columns, iterate over the 
> columns
> and set their widths - then you may need to send the table view a 
> tile message
> to sort itself out.
>
> Don't forget to set your max width on your columns to something 
> sensible.
>
>
> ( P G J H )
> _______________________________________________
>
> Cocoa-dev mailing list (<email_removed>)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>
> This email sent to <email_removed>

Related mailsAuthorDate
mlYann Disser Resizing NSTableView columns Peter Hudson May 11, 03:44
mlRe: Yann Disser Resizing NSTableView columns Corbin Dunn May 12, 19:12