Re: NSArrayController & image in column
Re: NSArrayController & image in column
- Subject: Re: NSArrayController & image in column
- From: Quincey Morris <email@hidden>
- Date: Tue, 19 Apr 2011 15:06:49 -0700
On Apr 19, 2011, at 14:26, Todd Heberlein wrote:
> I want to have an NSTableView controlled by an NSArrayController using an NSArray data source. In one of the columns I want to display one of several images. The choice of image will be based on an integer value in one of the fields of the array.
"one of the fields of the array"?? Do you mean a property of the objects in the array?
> Should I add to my NSArray object a field for an NSImage field (with getter/setter) and automatically load the image into the NSArray object, and then use that the getter for the NSImage as the key in the NSArrayController?
Well, yes, you could, but your terminology sucks. :)
You can add a property to the class of the objects in your array which returns the appropriate NSImage. In IB, drag a NSImageCell into the column that is going to be displaying images, and bind the table column via the array controller's arrangedObject to the array property that produces the NSImage.
Note that you're adding a read-only property, not a "field (with getter/setter)" (and you're *not* adding an instance variable, if that's what you meant).
Note that you're not going to "load the image into the NSArray object" -- you're going to make the image a property value of the NSArray object. I think that's what you meant. There's no real sense in which the images will be "in" the objects in the array.
Note that "the getter" isn't "the key in the NSArrayController". The array controller doesn't care about that part of the table column's key path. It's only responsible for providing the object that represents the row. The "key" is a property name in that object.
Dave already suggested an alternative, if you don't want to pollute the objects in the array with a UI-specific property.
Another alternative (and a slightly better one, I'd suggest, since it's more direct) is to provide the NSImage* directly via a data source. It works fine to use a data source for some columns but bindings for others.
> Should I subclass NSTableColumn and override
> -dataCellForRow:?
No need.
> Should I use a delegate for the NSTableView and override
> -tableView:dataCellForTableColumn:row:?
No need.
> Can I even use a custom NSTableColumn or NSTableView delegate with a table controlled by an NSArrayController?
No need.
These things would only be needed if you needed a different cell based on the row.
_______________________________________________
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