• 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: Dynamic columns in NSTableView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Dynamic columns in NSTableView


  • Subject: Re: Dynamic columns in NSTableView
  • From: Jeff Gilbert <email@hidden>
  • Date: Mon, 14 Nov 2005 22:19:03 -0600

Hi Lance,

On Nov 14, 2005, at 4:11 PM, Lance Roggendorff wrote:

Hi all,

I've searched many different resources for cocoa development on the internet, and I haven't found quite the answers I need. I apologize if this is a simple question.

I'm working on building a dialog which will have functionality similar to the import dialog in Excel, which will display a "preview" of tabular data. My version allows the user to select two columns to read data from.
I need to create columns for my table view dynamically, and i can't create columns in IB and hide them, because I don't know in advance how many columns there may be in any particular data file.


If anyone can point me in the right direction, I would really appreciate it. Thank you!

Lance

I use code similar to the following:

- (void)createTable
{
/* We need to remove the dynamic columns corresponding to the old data BEFORE we generate any new data. Removing columns forces a table update which will use cached outline items to get display values. If we dispose the old outline data then the outline has a dangling pointer (bad) */
[self removeDynamicColumns];


    [self addDynamicColumns];

[report setNeedsDisplay:YES]; // needed to refresh the number of columns displayed
[report setAutoresizesOutlineColumn:NO];
[report reloadData];
}



- (void)addDynamicColumns {

// determine the column names
// ...
while (columnName = [columnNameEnumerator nextObject])
{
// configure the new column
NSTableColumn* column = [self createTableColumnWithIdentifier:columnName title:columnName];


// add it to the table
[report addTableColumn:column];
[report moveColumn:([report numberOfColumns] - 1) toColumn:nextColumnIndex++];
[column release];
}
}



- (void)removeDynamicColumns { NSTableColumn* column;

// assume the dynamic columns are at the front of the table
column = [[report tableColumns] objectAtIndex:1];
while (![[column identifier] isEqualToString:StaticColumnIdentifier])
{
[report removeTableColumn:column];
column = [[report tableColumns] objectAtIndex:1];
}
}



- (NSTableColumn*)createTableColumnWithIdentifier:(NSString*) columnIdentifier title:(NSString*)columnTitle
{
// configure the new column
NSTableColumn* column = [[NSTableColumn alloc] initWithIdentifier:columnIdentifier];
[column setEditable:NO];


// we do not want the column to resize
// starting in 10.4, you should use setResizingMask: and not setResizable:
if ([column respondsToSelector:@selector(setResizingMask:)])
[column setResizingMask:NSTableColumnNoResizing];
else
[column setResizable:NO]; // pre-Tiger


[[column headerCell] setFont:[report headerFont]];
[[column headerCell] setStringValue:columnTitle];
[[column dataCell] setFont:[report tableFont]];
[self copyColumnConfiguration:column fromColumn:[report tableColumnWithIdentifier:PrototypeColumnIdentifier]];


    return column;
}


- (void)copyColumnConfiguration:(NSTableColumn*)dstColumn fromColumn: (NSTableColumn*)srcColumn
{
id srcCell;
id dstCell;


    srcCell = [srcColumn dataCell];
    dstCell = [dstColumn dataCell];
    [dstCell setAlignment:[srcCell alignment]];
    [dstCell setFormatter:[srcCell formatter]];

    srcCell = [srcColumn headerCell];
    dstCell = [dstColumn headerCell];
    [dstCell setAlignment:[srcCell alignment]];
}


Good luck, Jeff Gilbert _______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Dynamic columns in NSTableView (From: Lance Roggendorff <email@hidden>)

  • Prev by Date: Re: xcode 2.2 coredata accessors
  • Next by Date: [Meet] Cocoaheads New York City - Tuesday Nov 15th
  • Previous by thread: Dynamic columns in NSTableView
  • Next by thread: Re: Dynamic columns in NSTableView
  • Index(es):
    • Date
    • Thread