How to get custom table cell views into NSTableView?

2.9k views Asked by At

I have a NSTableView that uses mostly standard NSTextTableCellViews 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?

1

There are 1 answers

0
Ken Thomases On BEST ANSWER

You need to drop the new cell view in the table column that would contain that kind of cell view (the one whose identifier is "valueColumn", in this case).