How to update NSTableView without using -reloadData?

前端 未结 5 1762
猫巷女王i
猫巷女王i 2021-02-02 15:29

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 t

5条回答
  •  故里飘歌
    2021-02-02 15:38

    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))
        }
    }
    

提交回复
热议问题