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:

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:

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!
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.
datasourceCell Based to View BasedContent of the table view to ViewController > allPublishersValue of the Table View Cell of the first column to Table Cell View > objectValue.nameValue of the Table View Cell of the second column to Table Cell View > objectValue.idPlease note the subtle but important difference Table View Cell and Table Cell View
No further code required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With