Ale*_*c O 2 uitableview ios swift
我有一个标签栏应用程序和一个选项卡,其视图控制器由UITableView类控制.我有一个类用于从服务器下载数据,然后将其保存到NSDefaults.从那里我希望该类能够更新表视图,但不知道如何访问表视图的类来更新它.
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
}
}
Run Code Online (Sandbox Code Playgroud)
有两个区域注释//Indicates progress finished
我想从类中运行tableView:
class connectTableVC: UITableViewController {
...
}
Run Code Online (Sandbox Code Playgroud)
你可以使用protocol
或NSNotificationCenter
示例NSNotificationCenter
:
从UIViewController
下载数据开始,您可以在完成下载数据后发布通知,并告诉其他对象有新数据:
NSNotificationCenter.defaultCenter().
postNotificationName("newDataNotif", object: nil)
Run Code Online (Sandbox Code Playgroud)
并且任何其他想要收听此通知的对象都应该注册观察(在您的情况下UITableViewController
):
NSNotificationCenter.defaultCenter().
addObserver(self, #selector(shouldReload),
name:"newDataNotif", object: nil)
Run Code Online (Sandbox Code Playgroud)
然后你需要实现接收通知时要调用的方法:
func shouldReload() {
self.tableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)
请注意,通知也可以发送类型[NSObject : AnyObject]?
示例的数据:
NSNotificationCenter.defaultCenter().
postNotificationName("newDataNotif",
object: nil, userInfo: ["data":[dataArray]])
Run Code Online (Sandbox Code Playgroud)
观察者将像:
NSNotificationCenter.defaultCenter().
addObserver(self, #selector( shouldReload(_:)),
name:"newDataNotif", object: nil)
Run Code Online (Sandbox Code Playgroud)
而方法如:
func shouldReload(notification:NSNotification) {
println(notification.userInfo)
}
Run Code Online (Sandbox Code Playgroud)
最后deinit
你应该删除观察者:
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
Run Code Online (Sandbox Code Playgroud)
我认为最简单的方法是使用委托。
protocol UpdateDelegate {
func didUpdate(sender: Updates)
}
Run Code Online (Sandbox Code Playgroud)
因此,在您的更新类中,要刷新表视图的地方,将调用委托didUpdate函数。(您必须在开始对要调用的方法进行更新之前,必须设置委托。)weak关键字很重要,如果不存在,则两个类都不会被GC,这将导致内存泄漏。
class Updates {
weak var delegate: UpdateDelegate?
...
//Indicates progress finished
self.delegate.didUpdate(self)
Run Code Online (Sandbox Code Playgroud)
在tableView类中,您可以通过在类顶部添加UpdateDelegate来订阅该委托
class ConnectTableVC: UITableViewController, UpdateDelegate {
...
let updates = Updates()
updates.delegate = self
...
func didUpdate(sender: updates) {
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
Run Code Online (Sandbox Code Playgroud)
我建议不要使用NSNotificationCenter,最大的原因是任何地方的任何人都可以收听通知(在您的情况下,这不太可能,但仍然值得注意)。
归档时间: |
|
查看次数: |
5403 次 |
最近记录: |