• 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
Re: Sizing table columns to fit data - a solution looking for suggestions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Subject: Re: Sizing table columns to fit data - a solution looking for suggestions
  • From: Corbin Dunn <email@hidden>
  • Date: Fri, 30 Jan 2009 08:10:29 -0800

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

Overall, I would do it slightly differently; I would subclass NSTAbleHeaderView, figure out what column needs it to be autoresized and when (ie: double click), and then ask the delegate (optionally) what size it wants. But, I would make a default implementation based on -cellSize (possibly doing a monte carlo simulation on the number of rows, if there were a lot of rows, to help with performance).


Here are comments on your version:


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


- (void)tableView:(NSTableView *)tableView mouseDownInHeaderOfTableColumn:(NSTableColumn *)tableColumn {

First off, don't use the 'tableView:' prefix for your own delegate methods. Consider what would happen if AppKit introduced the same delegate method in a future release. Well, I can tell you from experience that it won't be good. So, please make it something like "scTableView:...".



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;

Use CGFloat not float.

// me don't like the next line
NSArray *theValues = [[myArrayController arrangedObjects] valueForKey:[thisColumn identifier]]; //dump the tableColumn values to an array


Instead of this hardcoded access, do this (sorry, not complete, but you should understand the idea):

CGFloat maxWidth = 0;
for (NSInteger i = 0; i < tableView.numberOfRows; i++) {
   NSCell *cell = [tableView preparedCellAtRow:i column:column];
   NSSize size = [cell cellSize];
   if (size.width > maxWidth) maxWidth = size.width;
}
[tableColumn setWidth:maxWidth];

The code above is *much* more generic. Note that it doesn't consider the indentation for outlineviews.

if ([thisColumn width] != newWidth) { [thisColumn setWidth:newWidth];

Setting the width in a loop is not good for performance; it might cause the table to relayout a lot of times.


Anyways, as usual, it never hurts to log feature requests for AppKit to do this for you.

corbin

} 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: Steve Cronin <email@hidden>
References: 
 >Sizing table columns to fit data - a solution looking for suggestions (From: Steve Cronin <email@hidden>)

  • Prev by Date: Re: NSFileHandle or a better way?
  • Next by Date: Re: Is there any Objective C supported code review tool?
  • Previous by thread: Sizing table columns to fit data - a solution looking for suggestions
  • Next by thread: Re: Sizing table columns to fit data - a solution looking for suggestions
  • Index(es):
    • Date
    • Thread