问题
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?
回答1:
Change your NSView
subclass to inherit from NSTableCellView
instead of NSView
, since NSTableCellView
also inherits from NSView
too.
回答2:
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.
来源:https://stackoverflow.com/questions/25186051/possible-to-use-nsview-subclass-in-nstableview