[英]How to get NSTableView to use a custom cell view mixed with preset cell views?
I have a NSTableView
configured in Interface Builder which uses several default cell views. 我在Interface Builder中配置了
NSTableView
,它使用了几个默认的单元格视图。 However the first column's cell view needs to be created from a custom class. 但是,第一列的单元格视图需要从自定义类创建。 How do I implement
NSTableViewDataSource
method tableView.viewForTableColumn()
so that it creates the default cell views for the remaining columns? 如何实现
NSTableViewDataSource
方法tableView.viewForTableColumn()
以便为其余列创建默认单元格视图?
Here's my method so far: 到目前为止,这是我的方法:
func tableView(tableView:NSTableView, viewForTableColumn tableColumn:NSTableColumn?, row:Int) -> NSView?
{
/* Create custom cell view for first column. */
if (tableColumn?.identifier == "nameColumn")
{
let view = tableView.makeViewWithIdentifier("nameCellView", owner: nil) as! NameTableCellView;
return view;
}
/* Return default cell views (defined in IB) for the rest. */
return tableView.viewAtColumn(??, row: row, makeIfNecessary: true); // How to get column index??
}
How do I get the right column index for tableView.viewAtColumn()
? 如何获取
tableView.viewAtColumn()
的正确列索引? tableView.columnForView()
or tableView.columnWithIdentifier()
are obviously not any option in this case. 在这种情况下,
tableView.columnForView()
或tableView.columnWithIdentifier()
显然不是任何选项。
I'd try just giving the default cell your own identifier in Interface Builder... 我会尝试在Interface Builder中为默认单元格提供自己的标识符...
...then just use that in conjunction with makeViewWithIdentifier:
: ...然后将其与
makeViewWithIdentifier:
结合使用:
func tableView(tableView: NSTableView,
viewForTableColumn
tableColumn: NSTableColumn?, row: Int) -> NSView? {
var viewIdentifier = "StandardTableCellView"
if let column = tableColumn {
switch column.identifier {
case "nameColumn":
viewIdentifier = "nameCellView"
default: break
}
}
return tableView.makeViewWithIdentifier(viewIdentifier, owner: self) as? NSView
}
I came across a good solution to the problem: You could make the table column identifiers to be the same of the corresponding view cells identifiers. 我遇到了一个很好的解决方案:您可以使表列标识符与相应的视图单元格标识符相同。 After that, you could use in your delegate:
之后,您可以在委托中使用:
- (NSView *)tableView:(NSTableView *)tableView
viewForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row {
NSTableCellView *cell = [tableView makeViewWithIdentifier:[tableColumn identifier] owner:NULL];
return cell;
}
From this starting point, you can easily take action for a specific column identifier and make another cellview instead according to your logic. 从这个起点开始,您可以轻松地针对特定的列标识符执行操作,并根据逻辑创建另一个单元格视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.