xcode 带有自定义单元格的 NSTableView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/910267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSTableView with custom cells
提问by Domness
it seems I've been searching for a long time and haven't found a great, easy, answer to my problem.
似乎我已经搜索了很长时间,但没有找到一个很好的、简单的、解决我的问题的方法。
I'm using XCode with Cocoa/ObjC and am trying to create an NSTableView which will load values from an NSDictionary/Array into different sections of a cell.
我正在将 XCode 与 Cocoa/ObjC 一起使用,并尝试创建一个 NSTableView,它将从 NSDictionary/Array 加载值到单元格的不同部分。
For example, I'm trying to get an NSImage, NSTextField and other items into a custom cell (along with a background image). However, I can't find a simple answer to how to create this..
例如,我试图将 NSImage、NSTextField 和其他项目放入自定义单元格(以及背景图像)。但是,我找不到如何创建它的简单答案..
I've been coding for the iPhone with UITableViews for a while now and can't seem to find a similar way with NSTableViews.
我已经使用 UITableViews 为 iPhone 编码了一段时间,但似乎找不到与 NSTableViews 类似的方法。
Any help would be really great!
任何帮助都会很棒!
Thanks
谢谢
Dominic
多米尼克
回答by Marc Charbonneau
NSTableView has -tableView:dataCellForTableColumn:row:
. Just create your NSCell subclass in your delegate and return it if you need customization for that row. If you're just using your custom cell for every row in the table view, you can also just use IB to set the custom cell class.
NSTableView 有-tableView:dataCellForTableColumn:row:
. 只需在您的委托中创建您的 NSCell 子类,如果您需要对该行进行自定义,则返回它。如果您只是为表格视图中的每一行使用自定义单元格,您也可以仅使用 IB 来设置自定义单元格类。
The table view will copy the cell as needed, so you can keep the cell as an instance variable if it would be more efficient. The data source methods or bindings on the table view work as they normally would, only you'll return your populated dictionary instead of a single string or number. You could also pass a custom model object directly to the tableview too, although you'll have to make it copyable, or override setObjectValue:
in your cell to wrap it in an NSValue.
表格视图将根据需要复制单元格,因此如果效率更高,您可以将单元格保留为实例变量。表视图上的数据源方法或绑定正常工作,只是您将返回填充的字典而不是单个字符串或数字。您也可以将自定义模型对象直接传递给 tableview,尽管您必须使其可复制,或者setObjectValue:
在您的单元格中覆盖以将其包装在 NSValue 中。
If it's subclassing NSCell that's giving you trouble, that can be a bit of a learning experience. Start with -drawWithFrame:inView:
to draw all your custom objects, and go from there as you need more functionality.
如果是 NSCell 的子类化给您带来了麻烦,那可能是一种学习经验。从-drawWithFrame:inView:
绘制所有自定义对象开始,然后在需要更多功能时从那里开始。
回答by Paul Solt
Watch the WWDC 2011 video "View Based NSTableView Basic to Advanced" (Session 120)
观看 WWDC 2011 视频“基于视图的 NSTableView 从基本到高级”(第 120 节)
https://developer.apple.com/videos/wwdc/2011/
https://developer.apple.com/videos/wwdc/2011/
This applies to Lion (10.7) and higher.
这适用于 Lion (10.7) 及更高版本。
回答by Peter Hosey
A cell can only hold one object value at a time. Make a model object with the image and string/attributed string as properties, and populate the table view with that.
一个单元格一次只能保存一个对象值。使用图像和字符串/属性字符串作为属性创建一个模型对象,并用它填充表视图。
Also, a text field is a view. Your model should know nothing of its presentation—that's your views' job.
此外,文本字段是一个视图。您的模型应该对其表示一无所知——这是您的视图的工作。