2

I want to fill my NSTableView with content. Per table-cell-row are 3 items (2 NSTextFields and 1 NSImageView). For that I created a custom NSTableCellView where I want to set the @IBOutlets of the 3 Items, to set there the value for them. But when I try to set the referencing outlets, the only option is to create an action.

When I try to write @IBOutlet weak var personName: NSTextfield and then set the references, I can't because "xcode cannot locate the class in the current workspace"

When I create the NSTableViewinside a main.storyboard, I'm able to set the outlet references. So what is the different behavior between .storyboard and .xib?

When I try to connect the @IBOutlet with the Item "Person Name" Troubleshoot

My NSViewController (owner of the .xib)

class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {

    @IBOutlet weak var tableView: NSTableView! //ref to tableView in xib
    var persons:[Person] = [] //content to fill tableview

    override func viewDidLoad() {
        super.viewDidLoad()

        persons.append(Person(name: "John", age: 23, piRef: "/Users/xy/Desktop/profilePic.png"))
        persons.append(Person(name: "Marie", age: 26, piRef: "/Users/xy/Desktop/profilePic.png"))


        tableView.delegate = self
        tableView.dataSource = self
    }


    func numberOfRows(in tableView: NSTableView) -> Int {        
        return persons.count        
    }

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

        let tableCellView:personTableCell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "defaultRow"), owner: self) as! personTableCell
//NSTableColumn in xib has id "defaulRow"

        if let person:Person = persons[row] {

            tableCellView.setPerson(person: person) //call method inside NSTableCellView-subclass to set item values
        }

        return tableCellView        
    }    
}


The custom NSTableCellView subclass ("personTableCell")

class personTableCell: NSTableCellView {

    var person:Person! = nil

    //here should be:
    //@IBOutlet weak var personName: NSTextField!
    //@IBOutlet weak var personAge: NSTextField!
    //@IBOutlet weak var personImg: NSImageView!

    func setPerson(person: Person) {

        self.person = person

        self.personName = person.name
        self.personAge = person.age
        self.personImg = NSImage(byReferencingFile: person.profileImgRef)

    }

}

I want to be able to add the item outlet references to my NSTableCellView-subclass.

Jozott
  • 2,367
  • 2
  • 14
  • 28

1 Answers1

2

It appears to me you're making this harder than it needs to be. makeView is giving you a reference to the cell. Therefore you can access its members directly. No need for outlets (which is why Xcode won't make them for you.)

I can't read your screenshots well enough to tell how the textfields are defined (old eyes), so I can only give you a generic example from a working demo of a custom cell class:

class DIYTableViewDelegate: NSObject, NSTableViewDelegate {
    var count = 0   // counts the number of views actually created

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        let id = tableColumn!.identifier
        var view = tableView.makeView(withIdentifier: id, owner: nil) as? CustomTableCellView
        if view == nil {
            view = createCell(id)
            count += 1
        }
        view!.textField!.stringValue = "\(id.rawValue) \(row) \(view!.count) \(count)"
        view!.count += 1

        return view
    }
}

Also, it's customary in Swift to capitalize the first letter of types (classes, structures, enums, protocols) and lowercase methods & properties. Doesn't affect how your code compiles, but it helps other Swifties read it.

Here's another example that may help:

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

        guard let vw = tableView.makeView(withIdentifier: tableColumn!.identifier, owner: self) as? CustomTableCellView else { return nil }

        vw.textField?.stringValue = String(pictures[row].dropLast(4))
        vw.imageView?.image = NSImage(named: pictures[row])

        return vw
    }
Ron
  • 660
  • 1
  • 5
  • 10
  • if I would have 2 Textfields in 1 Cell, how could I set the value of a specific one – Jozott Mar 28 '19 at 19:40
  • It would help if I could actually see how you define the fields in the storyboard. Sorry I can't read the screenshot. – Ron Mar 28 '19 at 19:44
  • Their identifier are "fileName" and "fileImg" or is there an other way to define them? – Jozott Mar 28 '19 at 19:57
  • Doesn't matter what you call them. You can reference them from the delegate as I show in my example. See edit to my original answer – Ron Mar 28 '19 at 20:01