Re: Custom cell in NSTableView
Re: Custom cell in NSTableView
- Subject: Re: Custom cell in NSTableView
- From: James Bucanek <email@hidden>
- Date: Tue, 31 May 2005 08:16:11 -0700
Gregory Olds II wrote on Monday, May 30, 2005:
>Hello all. I'm new to Cocoa programming and am trying to make a
>table view where all but the first column have a cell which contains
>2 text fields separated by a think black line. I'm not sure where to
>start to achieve this. Do I subclass NSCell and do some magic there,
>or do I just create a kind of custom NSView? I've looked at many
>examples but I can't quite figure out from them where to begin. Any
>help would be appreciated.
An NSTableView consists of a data model (where it gets the values to display), and a collection of NSTableColumn. Each column object has two NSCell objects: One for displaying the value of the column header and one for displaying the value of each cell in the column.
If all of your formatting can be easily done *inside* the boundries of an individual cell, then the easiest thing by far is to subclass NSCell (or some subclass of NSCell) to do the work.
Once you've created your NSCellSubclass, create an instance of it and set that cell object as the "dataCell" for each column by calling -[NSTableViewColumn setDataCell:(yourCustomCellObject)];
Two important things to keep in mind:
- There is only once instance of NSCell per column (unless you override NSTableColumn's dataCellForRow). That single instance is reused, over and over again, to draw the contents of every cell. This avoids the need to allocate an NSCell for every cell in the table. But it does mean that your cell has to be reusable and be efficient about it (i.e. no long, protracted, calculations during -[NSCell setObjectValue]).
- Your custom NSCell gets copied -- a lot. Make sure your custom NSCell conforms to the NSCopying protocol. (This bit me just last week.)
--
James Bucanek <mailto:email@hidden>
_______________________________________________
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