Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

ios - How to access and refresh a UITableView from another class in Swift

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
}

}

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

class connectTableVC: UITableViewController {
    ...
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use protocol or NSNotificationCenter

Example of NSNotificationCenter :

From the UIViewController that downloads the data, you can post a notification after finishing downloading the data and tell other objects that there is new data:

NSNotificationCenter.defaultCenter().
                    postNotificationName("newDataNotif", object: nil)

And any other object that wants to listen to this notification should register to observe (in your case the UITableViewController):

NSNotificationCenter.defaultCenter().
                     addObserver(self, #selector(shouldReload), 
                     name:"newDataNotif", object: nil)

and then you need to implement the method to be called when receive the notification:

func shouldReload() {
  self.tableView.reloadData()
}

Note that the notification can send data also of type [NSObject : AnyObject]? example:

NSNotificationCenter.defaultCenter().
                     postNotificationName("newDataNotif", 
                     object: nil, userInfo: ["data":[dataArray]])

And the observer will be like:

NSNotificationCenter.defaultCenter().
                     addObserver(self, #selector( shouldReload(_:)),
                     name:"newDataNotif", object: nil)

And the method like:

func shouldReload(notification:NSNotification) {
  println(notification.userInfo)
}

and finally in your deinit you should remove observer :

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...