简体   繁体   中英

How to get custom table cell views into NSTableView?

I have a NSTableView that uses mostly standard NSTextTableCellView s but I want some other cells that contain another UI component in my table in one or two rows. The question is: Where do I define those custom cells so that Cocoa finds them?

I just dropped a custom NSTableCellView into my XIB (same XIB in that the table is) and gave it an identifier. But then using this code it will obviously not find the cell ...

func tableView(tableView:NSTableView, viewForTableColumn tableColumn:NSTableColumn?, row:Int) -> NSView?
{
    var view:NSTableCellView?;
    if (tableColumn?.identifier == "labelColumn")
    {
        view = tableView.makeViewWithIdentifier("labelCell", owner: nil) as? NSTableCellView;
        view!.textField?.stringValue = _formLabels[row];
    }
    else if (tableColumn?.identifier == "valueColumn")
    {
        if (row == 1)
        {
            view = tableView.makeViewWithIdentifier("customCell", owner: nil) as? NSTableCellView;
            println("\(view)"); // nil - Not found!
        }
        else
        {
            view = tableView.makeViewWithIdentifier("valueCell", owner: nil) as? NSTableCellView;
            view!.textField?.stringValue = _formValues[row];
        }
    }

    return view;
}

All works but the cell with id customCell will not be found. How do I tell Cocoa to find it?

您需要将新的单元格视图拖放到包含该单元格视图的表列中(在这种情况下,其标识符为“ valueColumn”)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM