swiftcocoanstableviewnstablecellview

NSTableView displays empty view or placeholder "Table View Cell"


I'm trying to diplay list of static cell views in a NSTableView.

What I've done:

  1. Added identifier to NSTableColumn (Note)
  2. Added NSTableCellView from Object Library with identifier idNode
  3. Left pre-created Text Cell inside NSTableColumn (Note) as I can't delete it

enter image description here

Table view content mode — View Based.

Here's my code:

extension SidebarController: NSTableViewDataSource {
    func numberOfRows(in tableView: NSTableView) -> Int {
        return 1;
    }
    
    func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
        let cellIdentifier = NSUserInterfaceItemIdentifier(rawValue: "idNote")
        
        guard let cellView = tableView.makeView(withIdentifier: cellIdentifier, owner: self) as? NSTableCellView else {
            NSLog("Cant create cell view for row \(row)!")
            return nil
        }

        cellView.textField!.stringValue = "Hello, world!"

        return cellView
    }
}

tableView is:

  1. called
  2. no errors or short exits happens
  3. a valid cellView is returned (at least it seems so)

But I'm getting:

enter image description here

What I'm doing wrong?

FYI, if I delete identifier for NSTableCellView, I'm just getting Table Cell View placeholder.


Solution

  • The problem was I was using wrong overloaded function. The proper was:

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
            let cellIdentifier = NSUserInterfaceItemIdentifier(rawValue: "Note")
            
            guard let cellView = tableView.makeView(withIdentifier: cellIdentifier, owner: self) as? NSTableCellView else {
                NSLog("Cant create cell view for row \(row)!")
                return nil
            }
            
            cellView.textField!.stringValue = "Hello, world!"
            
            return cellView
        }
    

    So, note the difference:

    _ tableView: NSTableView, viewFor tableColumn
    

    The reason Xcode didn't allow me to use it — there was no NSTableViewDelegate in extension definition.

    This function confirms to NSTableViewDelegate. Only NSTableViewDataSource declaration is insufficient.

    So, to catch up:

    1. no NSTableViewDelegate
    2. no way to use proper function
    3. incorrect one was called anyway, so I didn't noticed the issue