Home > Enterprise >  Swift NSTableView delegates not being called
Swift NSTableView delegates not being called

Time:01-31

I'm pulling in some RSS feeds and trying to display them in a NSTableView. I am getting data returned as expected but when I call reloadData() the delegates/datsource methods do not get called.

Here is my code:

...
myRSSReader.parseFeed(completed: {(arrResults:Array<Any>) in
    if let arrArticles = arrResults as? Array<Dictionary<String, Any>> {
        self.arrArticles = arrArticles
                                
        DispatchQueue.main.async {
            print("Reloading table data...")
            self.tblArticles.reloadData()
        }
     }
})

The delegates are added to the viewcontroller

class ViewController: NSViewController, NSTextFieldDelegate, NSTableViewDelegate, NSTableViewDataSource {

the tableview is connected

@IBOutlet weak var tblArticles: NSTableView!

and I am delegating them...

tblArticles.delegate = self
tblArticles.dataSource = self

but they are not being called and I can't see where my error is

func tableView(tableView: NSTableView, numberOfRowsInSection section: Int) -> Int {
    return arrArticles.count
}

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
 
    guard let cellView = tableView.makeView(withIdentifier:
        NSUserInterfaceItemIdentifier(rawValue: "ArticleCell"),
        owner: self) as? NSTableCellView
    else {
        return nil
    }
    
    //compile data
    var strArticle = ""
    if let dictThisArticle = arrArticles[row] as? Dictionary<String, Any>, let strTitle = dictThisArticle["title"] as? String, let strDescription = dictThisArticle["description"] as? String, let strLink =  dictThisArticle["link"] as? String {
        strArticle = "\(strTitle)\n\(strDescription)\n\(strLink)\n\n"
    }
 
    cellView.textField?.stringValue = strArticle
    return cellView
}

Tried calling it from viewDidLoad and same results.

CodePudding user response:

func tableView(tableView: NSTableView, numberOfRowsInSection section: Int) -> Int

is a UITableViewDataSource method. The NSTableViewDataSource method is

func numberOfRows(in tableView: NSTableView) -> Int
  • Related