如何从Swift中的另一个类访问和刷新UITableView [英] How to access and refresh a UITableView from another class in Swift

查看:101
本文介绍了如何从Swift中的另一个类访问和刷新UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选项卡栏应用程序和一个选项卡,其视图控制器由UITableView类控制.我有一个用于从服务器下载数据,然后将其保存到NSDefaults的类.从那里,我希望该类能够更新表视图,但是不确定如何访问表视图的类以对其进行更新.

I have a tab bar application and a tab whose view controller is controlled by a UITableView class. I have a class that is used to download data from the server that then saves it to NSDefaults. From there I want the class to be able to update the table view, but am not sure how to access the table view's class to update it.

class updates {

func checkForUpdates() {
    //start on background thread
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { [unowned self] in
        //start contacting database:
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        let actualVersion = self.contactForUpdates()
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        if actualVersion > self.defaults.objectForKey("version") as! Int {
            //update data
            if self.downloadUpdates() {
                //update version number
                self.defaults.setObject(actualVersion, forKey: "version")
                //Indicates progress finished
                //self.stopSpinner()
            }
        }//end updates
        else {
            //Indicates progress finished
            //self.stopSpinner()

        }
    }//end background queue
}

}

我想从类中运行tableView的地方在//Indicates progress finished处有两个注释:

There are two areas commented //Indicates progress finished where I want to run tableView from the class:

class connectTableVC: UITableViewController {
    ...
}

推荐答案

我认为最简单的方法是使用委托.

I think the easiest way to do what you want to do is use a delegate.

protocol UpdateDelegate {
    func didUpdate(sender: Updates)
}

因此,在更新类中,要刷新表视图的地方,将调用委托didUpdate函数. (您必须在开始对要调用的方法进行更新之前设置委托.)weak关键字很重要,如果不存在,则两个类都不会被GC,这将导致内存泄漏

So in your updates class, where you want to refresh the table view you would call the delegates didUpdate function. (You will have to set the delegate before it starts the update for the method to be called.) The weak keyword is important, if it isn't there, neither of the classes will be GC'd, which will lead to memory leaks.

class Updates {
   weak var delegate: UpdateDelegate?

   ...

    //Indicates progress finished
    self.delegate.didUpdate(self)

在tableView类中,您可以通过在类顶部添加UpdateDelegate来订阅该委托

In your tableView class you would subscribe to that delegate by adding UpdateDelegate to the top of the class

class ConnectTableVC: UITableViewController, UpdateDelegate {
    ...

    let updates = Updates()
    updates.delegate = self

    ...
    func didUpdate(sender: updates) {
       dispatch_async(dispatch_get_main_queue()) {
          self.tableView.reloadData()
       }
    }
 }

我建议您不要使用NSNotificationCenter,最大的原因是任何地方的任何人都可以收听通知(在您的情况下这不太可能,但仍然值得注意).

I would recommend against using the NSNotificationCenter, the biggest reason is anyone anywhere can listen to the notification (which might be unlikely in your case, but it is still worth noting).

这篇关于如何从Swift中的另一个类访问和刷新UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆