0

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?

BadmintonCat
  • 9,416
  • 14
  • 78
  • 129
  • Where did you drop it? You should drop it in the table column that might contain that kind of cell view (the one whose identifier is "valueColumn", in this case). – Ken Thomases Jun 14 '15 at 15:33
  • Makes sense, after all tableView.makeViewWithIdentifier() is a method of the table view. I had it just loosely in my XIB at first. Dropping it in the table view column did work. Although I remember Xcode complained with warnings before when I tried this in the first table column that this is not supported. – BadmintonCat Jun 14 '15 at 15:49
  • I'm not sure exactly what the warnings may have been, but it's definitely supported and the proper approach. For example, if you drag a Source List from the object library, you get an outline view with two cell views in the column (one for the group rows and one for normal rows). – Ken Thomases Jun 14 '15 at 15:54
  • The warning I got before was this: `warning: Unsupported Configuration: Multiple table cell views using automatic identifier (Specify unique identifiers and implement the table view delegate method -tableView:viewForColumn:row: by using the table view method -makeViewForIdentifier: to get the view for each of these identifiers)` which sounds like Cocoa doesn't support dropping cells into a table like that (something `UITableView` has no trouble with). – BadmintonCat Jun 14 '15 at 16:33
  • Well, it supports it but it requires that you assign identifiers manually, which makes sense. Otherwise, how would you distinguish which cell view you wanted to use for a given row? – Ken Thomases Jun 14 '15 at 16:47
  • Well it did give me the warning even after I assigned identifiers to the cells. – BadmintonCat Jun 14 '15 at 17:02

1 Answers1

1

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).

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154