Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I needed to do this very thing and it turned out (for me at least) to be a bit more involved; this isn't perfect but pretty close ;-) p.s. Edited to handle image based cells</p> <pre><code>#define GBLBIN(x) [[NSUserDefaults standardUserDefaults] boolForKey:(x)] - (IBAction)autosizeColumns:(id)sender { NSMutableArray * widths = [NSMutableArray array]; NSIndexSet * rows = [table selectedRowIndexes]; NSArray * columns = [table tableColumns]; BOOL deslectAll = NO; NSNumber * col = nil; int i, j; // If no row's selected, then select all, the default if (![rows count]) { [table selectAll:sender]; rows = [table selectedRowIndexes]; deslectAll = YES; } // Use the selected rows from which we'll use the columns' formatted data widths for (i=[rows lastIndex]; i != NSNotFound; i=[rows indexLessThanIndex:i]) { for (j=0; j&lt;[columns count]; j++) { NSTableColumn * column = [columns objectAtIndex:j]; id item = [[table dataSource] tableView:table objectValueForTableColumn:column row:i]; NSCell * cell = [column dataCell]; float width, minWidth=10; // Depending on the dataCell type (image or text) get the 'width' needed if ([cell isKindOfClass:[NSTextFieldCell class]]) { NSFont * font = ([cell font] ? [cell font] : [NSFont controlContentFontOfSize:(-1)]); NSDictionary * attrs = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; NSFormatter * formatter = [cell formatter]; NSString * string = item; // We want a string, as IB would have formatted it... if (![item isKindOfClass:[NSString class]]) if (formatter) string = [formatter stringForObjectValue:item]; else string = [NSString stringWithFormat:@"%@", item]; width = [string sizeWithAttributes:attrs].width + minWidth; } else { // We have NSButtonCell, NSImageCell, etc; get object's width width = MAX(minWidth,[[cell image] size].width); } // First time seeing this column, go with minimum width if (j == [widths count]) { [widths addObject:[NSNumber numberWithFloat:minWidth]]; } col = [widths objectAtIndex:j]; width = MAX([col floatValue], width); [widths replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:width]]; } } // Now go resize each column to its minimum data content width for (j=0; j&lt;[widths count]; j++) { NSTableColumn * column = [columns objectAtIndex:j]; float width = [[widths objectAtIndex:j] floatValue]; if (GBLBIN(@"debug")) { NSLog(@"col:%d '%@' %.f", j, [column identifier], width); } [column setWidth:width]; } // Undo select all if we did it if (deslectAll) { [table deselectAll:sender]; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload