NSTableView中自定义NSCell

最近在研究NSTableView中自定义NSCell,找到一段非常有价值的文字,翻译如下:

An NSTableView consists of a data model (where it gets the values to display), and a collection of NSTableColumn. Each column object has two NSCell objects: One for displaying the value of the column header and one for displaying the value of each cell in the column. If all of your formatting can be easily done *inside* the boundries of an individual cell, then the easiest thing by far is to subclass NSCell (or some subclass of NSCell) to do the work. Once you’ve created your NSCellSubclass, create an instance of it and set that cell object as the “dataCell” for each column by calling -[NSTableViewColumn setDataCell:(yourCustomCellObject)]; Two important things to keep in mind: – There is only once instance of NSCell per column (unless you override NSTableColumn’s dataCellForRow). That single instance is reused, over and over again, to draw the contents of every cell. This avoids the need to allocate an NSCell for every cell in the table. But it does mean that your cell has to be reusable and be efficient about it (i.e. no long, protracted, calculations during -[NSCell setObjectValue]). – Your custom NSCell gets copied — a lot. Make sure your custom NSCell conforms to the NSCopying protocol. (This bit me just last week.)

一个NSTableView有如下几个部分组成:数据模型(也就是要显示的数据)和 NSTableColomn类组成的集合。一个NSTableColomn代表了表格中的一列,包括这一列的表格头。因此,一个NSTableColomn对象包括2个Cell,一个用来现实这一列的表格头,一个用来现实这一列的所有数据。

如果你的所有的数据都可以简单的在一个单独的cell中完成,那之要简单的继承NSCell就好。(这里我也不是太明白)。

一旦创建了NSCell的子类,就可以创建一个实例,然后将这个实例赋值给NSTableView,通过这个函数:

-[NSTableViewColumn setDataCell:(yourCustomCellObject)];

以下两点非常重要:

1. 对于每一列,有且只有一个NSCell的实例。这个实例不停的被重用,以便将所有的数据显示出来,这样可以避免对每一个表格的数据申请一个内存空间。

2. 对于custom的NSCell,在整个运行过程中会大量的被copie。所以要保证custom的NSCell实现了NSCopy.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值