How to update NSTableView without using -reloadData?

Viewed 14018

I am new to the Mac. I am trying to update a particular cell in NSTableView without using -reloadData, as -reloadData updates the whole table. I have tried everything but all was in vain. I am trying to do something similar to what we used to do in CListCtrl in MFC or in .NET.

5 Answers

If your table row is smart enough to update itself with a draw, you can do something like this. This ViewController method is responding to a NSNotification post by a source object that has updated itself.

Swift 4 code:

class ObjectChooserListViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {

    @IBOutlet weak var tableView: NSTableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(objectUpdating(_:)), name: NSNotification.Name("ObjectUpdating"), object: nil)
        // more stuff happening
    }

    @objc func objectUpdating(_ notification: NSNotification) {
        guard let object = notification.object as? MyCoolObject else {return}
        guard let row = ObjectManager.shared.getRow(of: object.id) else {return}
        guard let cellView = tableView.view(atColumn: 0, row: row, makeIfNecessary: false) else {return}
        cellView.draw(tableView.frameOfCell(atColumn: 0, row: row))
    }
}
Related