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 (Cocoa-dev@lists.apple.com)

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/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to