[英]How to get NSTableView to use a custom cell view mixed with preset cell views?
我在Interface Builder中配置了NSTableView
,它使用了幾個默認的單元格視圖。 但是,第一列的單元格視圖需要從自定義類創建。 如何實現NSTableViewDataSource
方法tableView.viewForTableColumn()
以便為其余列創建默認單元格視圖?
到目前為止,這是我的方法:
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??
}
如何獲取tableView.viewAtColumn()
的正確列索引? 在這種情況下, tableView.columnForView()
或tableView.columnWithIdentifier()
顯然不是任何選項。
我會嘗試在Interface Builder中為默認單元格提供自己的標識符...
...然后將其與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
}
我遇到了一個很好的解決方案:您可以使表列標識符與相應的視圖單元格標識符相同。 之后,您可以在委托中使用:
- (NSView *)tableView:(NSTableView *)tableView
viewForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row {
NSTableCellView *cell = [tableView makeViewWithIdentifier:[tableColumn identifier] owner:NULL];
return cell;
}
從這個起點開始,您可以輕松地針對特定的列標識符執行操作,並根據邏輯創建另一個單元格視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.