NSTableView与自定义单元格 [英] NSTableView with custom cells

查看:114
本文介绍了NSTableView与自定义单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎我一直在寻找很长时间,并没有找到一个伟大,容易,回答我的问题。



我使用XCode与Cocoa / ObjC,我试图创建一个NSTableView,它将从NSDictionary / Array中的值加载到单元格的不同部分。



例如,我试图获得NSImage,NSTextField和其他项目转换为自定义单元格(连同背景图像)。但是,我找不到一个简单的答案,如何创建这个..



我已经为UITableViews的iPhone编码了一段时间,不能似乎找到一个类似的方式与NSTableViews。



任何帮助将是真的很棒!



Dominic

解决方案

NSTableView有 -tableView:dataCellForTableColumn :row:。只需在您的委托中创建您的NSCell子类,并返回它,如果你需要自定义该行。如果您只是在表视图中为每一行使用自定义单元格,您也可以只使用IB来设置自定义单元格类。



表视图将复制根据需要单元格,所以你可以保持单元格作为一个实例变量,如果它会更有效率。表视图上的数据源方法或绑定按照通常的方式工作,只有返回填充的字典,而不是单个字符串或数字。你也可以将一个自定义模型对象直接传递给tableview,虽然你必须让它可复制,或者覆盖你的单元格中的 setObjectValue:,将它包装在NSValue 。



如果它是NSCell的子类,这给你的麻烦,这可以是一点点学习的经验。以 -drawWithFrame:inView:开始绘制所有自定义对象,然后从那里开始,因为您需要更多功能。


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.

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..

I've been coding for the iPhone with UITableViews for a while now and can't seem to find a similar way with NSTableViews.

Any help would be really great!

Thanks

Dominic

解决方案

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.

这篇关于NSTableView与自定义单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆