如何在外部类中执行tableView方法reloadData()



我想在外部类中执行self.tableView.reloadData(),而不是在tableViewController中。我发现我必须在课堂上做一个自定义委托,但我不知道怎么做!有人可以用Swift中的一些代码示例向我展示如何做到这一点,谢谢!

您可以编写您的协议,也可以像这样简单地使用NSNotification

class xViewController: UIViewController {
    let myTableView = UITableView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height), style: .Plain)
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadMyTableView:", name: "updateNotification", object: nil)
    }
    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        NSNotificationCenter.defaultCenter().removeObserver(self, name: "updateNotification", object: nil)
    }
    func reloadMyTableView(notification: NSNotification) {
        myTableView.reloadData()
    }
}

并从您想要重新加载的位置发布您的通知;

class yViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().postNotificationName("updateNotification", object: nil)
    }
}

最新更新