NSTableView with custom cells

随声附和 提交于 2019-11-28 17:57:53

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.

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.

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.

Paul Solt

Watch the WWDC 2011 video "View Based NSTableView Basic to Advanced" (Session 120)

https://developer.apple.com/videos/wwdc/2011/

This applies to Lion (10.7) and higher.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!