Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode Array to NSTableView

I have done some searching, and some coding, some more searching, and then some more coding. I just can't get this to work on my own yet. I would like to get data from an array I have defined:

var allPublishers = [[String: String]]()

I will be filling up the array in chunks from an external API call. I can call a refresh every 100 chunks. Here is the population:

for i in 0 ..< json["publishers"].count {
    let pname: String = String(describing: json["publishers"][i]["publisher"]["account_name"])
    let pid: String = String(describing: json["publishers"][i]["publisher"]["publisher_id"])
    self.allPublishers.append(["name": pname, "id": pid])
}

For example, after the first 100 I call this function:

self.showPublishers()

Which for now just shows the type, for these purposes:

func showPublishers(){
    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
}

Type is: Array<Dictionary<String, String>>

A sample of a partially populated chunk is:

[["id": "1100l6141", "name": "014loyalia"], ["id": "1101l11949", "name": "030magazin"]]

I have an outlet to my NSTableView set up:

@IBOutlet weak var tableView: NSTableView!

Which is in my Main.storyboard:

NSTableView

I have no bindings on this as of yet, aside from the ViewController.swift outlet.

When I execute the program, the TableView updates the rows, but not with the values from the array:

Rows

Here are my tableView functions:

extension ViewController:NSTableViewDataSource{
    func numberOfRows(in tableView: NSTableView) -> Int {
        return allPublishers.count
    }

    func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
        return allPublishers[row][(tableColumn?.identifier.rawValue)!]
    }
}

Can anyone advise on how to populate this NSTableView with the actual data from allPublishers please?

Thanks in advance!

like image 903
webmasterjunkie Avatar asked Jul 25 '26 03:07

webmasterjunkie


1 Answers

You are strongly discouraged from using a cell based table view.

Cocoa Bindings and a view based table view are very easy to implement.

  • Declare allPublishers as dynamic

    @objc dynamic var allPublishers = [[String: String]]() // however a custom class is highly recommended.
    
  • In Interface Builder
    • Disconnect datasource
    • Change the table view from Cell Based to View Based
    • Bind the Content of the table view to ViewController > allPublishers
    • Bind the Value of the Table View Cell of the first column to Table Cell View > objectValue.name
    • Bind the Value of the Table View Cell of the second column to Table Cell View > objectValue.id

Please note the subtle but important difference Table View Cell and Table Cell View

No further code required.

like image 102
vadian Avatar answered Jul 27 '26 18:07

vadian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!