开发者

Cocoa Bindings and Custom NSCell properties

I have a problem regarding subclassed NSCells (Check Box Cells). I want them to have a title开发者_Go百科 and a subtitle next to the checkmark (so I created NSString properties for these). Their values shall be set via Bindings (from the IB would be awesome) but here comes the problem:

IB only allows me to set the "value" of the cells, in my case a BOOL for the checkmark state.

What class has to be bound to the NSArrayController that is serving the data? It seems like the cells themselves cannot be bound to anything and doing things like

[self.subtitle bind:...]

don't work either. Where and when do I have to do the binding?

Best, Stefan


I'm going to assume that you're doing this in a cell-based NSTableView, because NSButtonCell does expose a title binding, but it's useless in the context of an NSTableView, because you have to bind the NSTableColumn, and that only exposes a Value binding for NSTableColumns containing NSButtonCells. It's a shortcoming of the framework, and one that Google reveals that many have grappled with.

One approach to this is to update the title from the model in your -tableView:dataCellForTableColumn:row: and -tableView:willDisplayCell:forTableColumn:row: delegate methods. This has the drawback of not being updated via KVO when the underlying model changes. If that's important for your application, one workaround is to make the model keyPath for the value of the checkbox dependent on the model keyPath for the title of the checkbox. This way, when the title changes, KVO will send notifications for a change to the value, which will cause the row to be re-drawn, and thus the title will be updated by the delegate method.

I notice that this question is old, but an easier approach these days would be to use NSView-based NSTableViews. When done that way, you're working with a first-class NSButton control, with both Value and Title bindings (and a bunch of others) available. It's often a more workable approach, assuming you're not worried about supporting OS version prior to Lion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜