• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Sizing table columns to fit data - a solution looking for suggestions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Sizing table columns to fit data - a solution looking for suggestions


  • Subject: Sizing table columns to fit data - a solution looking for suggestions
  • From: Steve Cronin <email@hidden>
  • Date: Thu, 29 Jan 2009 22:37:03 -0600

Folks;

iTunes and Excel both have a feature wherein if you double-click on the column divider in the tableHeader then the table column width is adjusted to the maximum width of the data in the column.

Below is a reasonably generic Cocoa solution for implementing this behavior.
The tableView I developed this for uses an arrayController with individual column bindings; not the old school -dataSource methods for handling the data.
This code should exist in tableView's delegate.
This solution handles both strings and images.
I don't like the reliance on calling the arrayController directly but for now it serves my purpose


I am interested in both criticisms and improvements - both will serve the Cocoa community..

- (void)tableView:(NSTableView *)tableView mouseDownInHeaderOfTableColumn:(NSTableColumn *)tableColumn {
NSEvent *event = [NSApp currentEvent];
if ([event clickCount]==2) {
NSPoint location = [event locationInWindow];
NSTableHeaderView *header = [tableView headerView];
location = [header convertPoint:location fromView:nil];
if (location.x>=2.0) location.x-=2; // offset point 2 pixels 'cause the 'split' cursor is a little 'permissive'
NSTableColumn *thisColumn = [[tableView tableColumns] objectAtIndex: [header columnAtPoint:location]];
NSString *maxStr = @"";
id thisObj;
NSCell *dCell;
float newWidth, maxStringWidth = 0.0 ,maxImageWidth = 0.0;
// me don't like the next line
NSArray *theValues = [[myArrayController arrangedObjects] valueForKey:[thisColumn identifier]]; //dump the tableColumn values to an array
if ((theValues!=nil) && ([theValues count]>0)) {
NSEnumerator *valEnum = [theValues objectEnumerator];
// find the longest string or widest image
while ((thisObj = [valEnum nextObject]) != nil) {
if ([thisObj isKindOfClass:[NSString class]]) {
if ([thisObj length] > [maxStr length]) maxStr = thisObj; // keep the string 'cause later we use dataCell to get -width
} else if ([thisObj isKindOfClass:[NSImage class]]) {
if ([thisObj size].width > maxImageWidth) maxImageWidth = [thisObj size].width;
}
}
//below covers the case where a mix of strings and images were found - chooses the wider of the two
if ([maxStr length]> 0) {
dCell = [thisColumn dataCell];
[dCell setStringValue:maxStr];
maxStringWidth = [dCell cellSize].width;
}
newWidth = (maxStringWidth > maxImageWidth) ? maxStringWidth : maxImageWidth;
//this check prevents anything from happing unless we have found something actionable
if (newWidth>0) {
//bump up max if necessary
if (newWidth>[thisColumn maxWidth]) [thisColumn setMaxWidth:newWidth];
//set width to max if not already max
if ([thisColumn width] != newWidth) {
[thisColumn setWidth:newWidth];
} else {
//if already max then here double-click sets to half max! (maybe NOT do this if images were found?)
[thisColumn setWidth:newWidth/2.0];
}
}
}
}
}


_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Sizing table columns to fit data - a solution looking for suggestions
      • From: Corbin Dunn <email@hidden>
  • Prev by Date: Re: Partially Transparent windows
  • Next by Date: Re: URL Parsing
  • Previous by thread: Is the NSData used to create a NSImage copied or retained?
  • Next by thread: Re: Sizing table columns to fit data - a solution looking for suggestions
  • Index(es):
    • Date
    • Thread