views:

2611

answers:

2

Hello there, it seems I've been searching for a long time and haven't found a great, easy, answer to my problem.

I'm using XCode with Cocoa/ObjC and am trying to create an NSTableView which will load values from an NSDictionary/Array into different sections of a cell.

For example, I'm trying to get an NSImage, NSTextField and other items into a custom cell (a long with a background image). However, I can't find a simple answer to how to create this..

I've been coding for the iPhone with UITableViews for a while now and can't seem to find a similar way with NSTableViews.

Any help would be really great!

Thanks

Dominic

+8  A: 

NSTableView has -tableView:dataCellForTableColumn:row:. Just create your NSCell subclass in your delegate and return it if you need customization for that row. If you're just using your custom cell for every row in the table view, you can also just use IB to set the custom cell class.

The table view will copy the cell as needed, so you can keep the cell as an instance variable if it would be more efficient. The data source methods or bindings on the table view work as they normally would, only you'll return your populated dictionary instead of a single string or number. You could also pass a custom model object directly to the tableview too, although you'll have to make it copyable, or override setObjectValue: in your cell to wrap it in an NSValue.

If it's subclassing NSCell that's giving you trouble, that can be a bit of a learning experience. Start with -drawWithFrame:inView: to draw all your custom objects, and go from there as you need more functionality.

Marc Charbonneau
Thanks for the info! I found very few documentation regarding NSCells.
Martin Cote
If you are designing a custom cell that has instance variables, read carefully the documentation, as some methods MUST be implemented like "copyWithZone:".
Laurent Etiemble
+2  A: 

A cell can only hold one object value at a time. Make a model object with the image and string/attributed string as properties, and populate the table view with that.

Also, a text field is a view. Your model should know nothing of its presentation—that's your views' job.

Peter Hosey