iphone007
iphone007

Reputation: 503

What to return from tableView:objectValueForTableColumn:row:?

New to Cocoa programming on OSX. Came from iOS programming. It seems like NSTableView programming is similar to UITableView programming. So I'm trying to implement a part of the NSTableViewDataSource protocol but I don't know what to return from the tableView:objectValueForTableColumn:row:method in order to get a simple string to display on a row and column. I've googled, skimmed the Apple documentation and can't find anything. The method returns type "id" so that's no help. Thanks.

Upvotes: 5

Views: 2133

Answers (2)

Ken Thomases
Ken Thomases

Reputation: 90571

The object value that you return depends on a) what makes sense for your model, and b) how you have set up the cell views of your table view.

I assume you're using a view-based table view, since you're new and that's the modern way. So, the table view takes the object value returned by -tableView:objectValueForTableColumn:row: and tries to set it on the cell view. It checks if the cell view implements -setObjectValue: and, if so, calls it and passes the object value that was returned from the data source.

So, what sorts of views support -setObjectValue: and what do they do with it when it's set? Well, any view that inherits from NSControl has such a method and displays and, potentially, let's the user edit the value. A common example of such a control is NSTextField. So, if your cell view is an NSTextField or other control, your data source could return an NSString and that would be good enough.

If the control has a formatter object (instance of a subclass of NSFormatter), your data source could return some other type of object, such as an NSNumber or NSDate, and the formatter would convert it to and from text.

If your cell view is an NSTableCellView, that class also responds to -setObjectValue:. So the table view would call it with the object value returned from the data source. However, an NSTableCellView does not inherently do anything with the object value. It just holds it. What you can then do is have the subviews bind to it through the objectValue property. They will get their values through the binding. In that case, the data source can return an arbitrary object. The subviews would select specific properties of the object to display and edit by selecting an appropriate key path for the binding. For example, if your app manipulates Employee objects, your data source could return the Employee for the row, the NSTableCellView would hold a reference to it, and a text field within the cell view could bind to the NSTableCellView with a key path of objectValue.name to show the employee's name. (You can further control the translation from object properties to displayed value using bindings options, like a value transformer, or a formatter on the control.)

Upvotes: 11

Rupert Pupkin
Rupert Pupkin

Reputation: 790

If you want a string displayed, return a NSString* such as:

- (id)tableView:(NSTableView*)sender objectValueForTableColumn:(NSTableColumn*)tableColumn row:(int)rowIndex
{
return [NSString stringWithFormat:@"Row: %d , Col: %@", rowIndex, [tableColumn identifier]];
}

Upvotes: -1

Related Questions