为什么仅调用 setObjectValue:nil 时 NSPopUpButtonCell 就能正确显示?
一个NSTableView
有多个NSCell
(间接通过NSTableColumn
)。 NSCell遵循Flyweight设计模式;他们都是千篇一律的人。每列有一个单元格,并要求它为每一行绘制自己。 “这是一个值,将其绘制在第一行;这是第二行的值,等等。”这与基于视图的表格视图形成鲜明对比,基于视图的表格视图每个单元格都有一个视图实例。
我对 NSPopUpButtonCell 进行了子类化,并且对于每个 setXxxValue:
方法,我调用 NSLog();
并将消息转发到超类。唯一被调用的方法(例如,添加新行或刷新表时)是 setObjectValue:
。这是预期的 文档...但 null
是唯一传递给 它!!应向其传递一个带有所选项目索引的 NSNumber。
我的表格画得很好。我只是想理解这一点,因为我想扩展/更改单元格的行为(显然,否则我不需要子类)。
问题:这怎么可能?如何将 NSPopUpButtonCell 设置为一个值以便为每行绘制正确的值。即使考虑绑定,也是 NSTableColumn 承担着繁重的工作,不是吗?我缺少什么?
An NSTableView
has multiples NSCell
s (indirectly via NSTableColumn
). NSCell follows the Flyweight design pattern; they're cookie-cutters. There's one cell per column and it's asked to draw itself for each row. "Here's a value, draw it on the first row; here's the value for the second row, etc." This is in contrast to view-based tableviews which have a view instance per cell.
I've subclassed NSPopUpButtonCell and for each setXxxValue:
method, I call NSLog();
and forward the message to the super class. The only method which gets called (say, when adding new rows, or otherwise refreshing the table) is setObjectValue:
. This is expected from the documentation... but null
is the only thing passed to it!! It should be passed an NSNumber with the selected item index.
My table draws fine. I'm just trying to understand this because I want to extend/change the behaviour of the cell (obviously as I would'nt have needed a subclass otherwise).
Question: How is this possible? How is the NSPopUpButtonCell being set to a value in order to draw the right value for each row. Even considering bindings, it's NSTableColumn doing the heavy lifting, no? What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“表中的每一行共享相同的弹出菜单”——情况不一定如此。
除了设置表列的值绑定之外,还可以通过数组控制器将单元格的内容绑定设置为模型对象的数组属性。
您可以保留确定模型中选项的代码(只需创建一个字符串数组)。这样您就不需要自定义子类。
"Every row in a table shares the same popup menu" – that doesn't need to be the case.
In addition to setting the table column's value binding, set the cell's content binding to an array property of your model object, via your array controller.
You can keep the code which determines the options in the model (just create an array of strings). That way you won't need a custom subclass.