简体   繁体   中英

JSON data not displaying in NSTableView

I am fairly new to Swift but I have a NSTableView that is not displaying any of my data. My JSON data is being printed in the console perfectly and I thought that my cellView would display in my textField all my values but I get nothing back. I have my Table set up to where my 'Table Cell View' has an identifier of 'cell' so I believe they are linked correctly. I am not receiving any errors in the console but my data is still not displaying. Any help would be greatly appreciated.

import Cocoa

class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {

@IBOutlet var tableView: NSTableView!

var values: NSArray = []

override func viewDidLoad() {
    super.viewDidLoad()

    get();


}


override var representedObject: Any? {
    didSet {

    }
}

func get(){

    let url = NSURL(string: "http://myurl")

    let data = NSData(contentsOf: url as! URL);

    values = try! JSONSerialization.jsonObject(with: data! as Data,options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray

    tableView.reloadData();


    print(values);

}

func numberOfRows(in tableView: NSTableView) -> Int {

    return self.values.count;
}

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

    let cellView = tableView.make(withIdentifier: "cell", owner: self) as! NSTableCellView

    cellView.textField!.stringValue = self.values.object(at: row) as! String


    return cellView
}

did you make sure your table view knows it's delegate and data source?, if not, add this to viewDidLoad

self.tableView.delegate = self
self.tableView.dataSource = self

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM