Re: [Cocoapsql-developer] commit; moved autoSizeTableColumns to Category
Status: Alpha
Brought to you by:
alkirkus
|
From: Al K. <al...@ki...> - 2002-06-09 00:50:54
|
Hi Olaf.
I was thinking about the tableview and autosizing the columns. Is there
any reason that we can not autosize the columns as needed instead of
autosizing against the first x columns when the table is initially
setup. What I mean is why not just autosize for the currently listed
records and when someone scrolls check the sizes as the rows are being
fetched from the datasource. I need to think about the details but this
seems like a far more efficient and accurate way of dealing with the
problem.
Thoughts?
Al
On Saturday, June 8, 2002, at 01:24 PM, Olaf van Oudenallen wrote:
> Moved the autoSizeColums from PSQLController.m to a new Catergory
> PSQLTableViewCat so the method will be available to all NSTableView's.
> Modified the method to be prepaired for the use of a formatter on
> NSTableColumn's dataCell (not tested!). Further the method name is
> altered and can point to one column or all (-1). The maximum number of
> rows to be processed is part of the method name as well.
> "PSQLTableViewCat" Grouped these files and the PSQLTextView files to
> the new Group "ClassExtensions" in the project.
> Added the choice to autosize one or all columns.
>
> Added to PSQLController.m #import "PSQLTableViewCat.h"
> Modified PSQLController submitQuery and setTableShouldAutoSizeColums:
> [self autoSizeTableColumns];
> now is
> [tableView autoSizePSQLTableColumn:-1 maxRows:200];
>
> PSQLTableViewCat.h
> #import <AppKit/NSTableView.h>
>
> @interface NSTableView ( PSQLTableView )
> - (void)autoSizePSQLTableColumn:(int)cn maxRows:(int)r;
> @end
>
> PSQLTableViewCat.m
> #import "PSQLTableViewCat.h"
> #import <Cocoa/Cocoa.h>
>
> @implementation NSTableView ( PSQLTableView )
> - (void)autoSizePSQLTableColumn:(int)cn maxRows:(int)r {
> NSFont *dataCellsFont;
> NSDictionary *attributes;
> NSSize cellToCellSpacing, cellsSize;
> float cellWidthFitsAll;
> int i, j, firstColumn, lastColumn;
> NSArray *curColumns = [self tableColumns];
> id curColId;
> id datSo = [self dataSource];
> id formatter;
> NSString *valueWithFormat;
> // need access to dataSource's method
> tableView:objectValueForTableColumn:row:
> SEL targetMethod =
> @selector(tableView:objectValueForTableColumn:row:);
> if ( ![datSo respondsToSelector:targetMethod] ) {
> NSLog(@"PSQLTableViewCat: need
> tableView:objectValueForTableColumn:row: \n for calculating column
> width");
> return;
> }
>
> lastColumn = [curColumns count];
>
> // process one or all columns (or none)
> if ( cn >= 0 ) {
> if ( cn > lastColumn -1 ) {
> NSLog(@"columnNumber grater than number of columns -1");
> return;
> } else {
> firstColumn = cn;
> lastColumn = firstColumn;
> }
> } else {
> firstColumn = 0;
> }
>
> cellToCellSpacing = [self intercellSpacing];
> for(i=firstColumn;i<lastColumn;i++) {
> // headerCell
> curColId = [curColumns objectAtIndex:i];
> dataCellsFont = [[curColId dataCell] font];
> if ( dataCellsFont == nil ) {
> // dataCellsFont = nil if headerCell is not a text-type cell
> // set column width to reasonable minimum
> cellWidthFitsAll = 10;
> } else {
> cellsSize = [[curColId headerCell] cellSize];
> cellWidthFitsAll = cellsSize.width;
> }
>
> // dataCell for all rows
> dataCellsFont = [[curColId dataCell] font];
> if ( dataCellsFont == nil ) {
> // dataCellsFont = nil if dataCell is not a text-type cell
> // leave column width as it is
> } else {
> attributes = [NSDictionary dictionaryWithObject:NSFontAttributeName
> forKey:[dataCellsFont fontName]];
> formatter = [[curColId dataCell] formatter];
> for(j=0;r>j && j<[self numberOfRows];j++) {
> if (formatter)
> valueWithFormat = [formatter
> stringForObjectValue:[datSo tableView:self
> objectValueForTableColumn:curColId row:j]];
> else valueWithFormat = [datSo tableView:self
> objectValueForTableColumn:curColId row:j];
> /* PSQLQuery's tableView:objectValueForTableColumn:row: returns an
> NSCFString */
> cellsSize =[valueWithFormat sizeWithAttributes:attributes];
> }
> if ( cellsSize.width > cellWidthFitsAll ) {
> cellWidthFitsAll = cellsSize.width;
> }
> }
>
> // set column width
> [curColId setWidth:
> ((cellWidthFitsAll+cellToCellSpacing.width+0)*1.2)];
> /* added 0 for a border. corrected with factor 1.2 for an
> undefined fault */
> }
> [self setNeedsDisplay:YES];
> }
> @end
>
>
>
> _______________________________________________________________
>
> Don't miss the 2002 Sprint PCS Application Developer's Conference
> August 25-28 in Las Vegas -
> http://devcon.sprintpcs.com/adp/index.cfm?source=osdntextlink
>
> _______________________________________________
> Cocoapsql-developer mailing list
> Coc...@li...
> https://lists.sourceforge.net/lists/listinfo/cocoapsql-developer
|