martinjbaker
martinjbaker

Reputation: 1484

Possible to use NSView subclass in NSTableView?

I have an NSView subclass that I'd like to use in an NSTableView. Normally of course I'd use a NSTableCellView subclass but this view will be used elsewhere.

Possible?

Upvotes: 0

Views: 450

Answers (2)

Ken Thomases
Ken Thomases

Reputation: 90521

There's no requirement that the cell view used for a view-based table view be an NSTableCellView or subclass. It's perfectly OK to use an instance of some other view object. For example, an NSTextField works out of the box.

Your table view delegate's -tableView:viewForTableColumn:row: method can configure the view as appropriate for the row. Or, your view can implement an objectValue property (with -setObjectValue: setter). In this case, the table view will forward whatever object value your data source returns from its -tableView:objectValueForTableColumn:row: method to your view's objectValue property.

Also, if your view class has textField or imageView properties (like NSTableCellView does), then those will be treated specially in certain cases. For example, the text field will be the accessibility string for the cell.

Basically, all of the behaviors of NSTableCellView are generalizable. It's not that the class is treated particularly specially by the framework. It's that it provides the appropriate properties and methods and any view with those same properties and methods could replicate its behavior.

Upvotes: 2

TheAmateurProgrammer
TheAmateurProgrammer

Reputation: 9392

Change your NSView subclass to inherit from NSTableCellView instead of NSView, since NSTableCellView also inherits from NSView too.

Upvotes: 1

Related Questions