每 30 秒重新加载表视图 swift 中的数据
Reload data in swift, of a tableview every 30 seconds
所以我有一个 table 从我的数据库中获取一些数据并将其显示在他的单元格中。我制作了一个刷新按钮来重新加载我的代码,这样当您点击它时,用户就可以看到有人添加到数据库中的新条目。
这就是我的刷新按钮的代码:
@IBAction func refreshButton(sender: AnyObject) {
textArray.removeAllObjects()
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Conversation", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
publicCloudDatabase?.performQuery(query, inZoneWithID: nil,
completionHandler: ({results, error in
if (error != nil) {
dispatch_async(dispatch_get_main_queue()) {
self.notifyUser("Cloud Access Error",
message: error.localizedDescription)
}
} else {
if results.count > 0 {
var record = results[0] as! CKRecord
self.currentRecord = record
dispatch_async(dispatch_get_main_queue()) {
for x in results{
self.textArray.addObject(x.objectForKey("message") as! String)
self.tableView.reloadData()
}
}
} else {
dispatch_async(dispatch_get_main_queue()) {
self.notifyUser("No Match Found",
message: "No record matching the address was found")
}
}
}
}))
}
我想删除该按钮并每 30 秒刷新一次 table(在应用程序的后台 - 我不希望用户知道它),它那可能吗 ?如果可以,怎么办?
谢谢!
1.performQuery 每 30 秒。
2.compare旧数据与新数据的区别
3.Insert 使用这个 Api
的不同部分
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
而不是重新加载数据
使用计时器是您可以实现的一种方式,
在 ViewDidload
self.myTimer = NSTimer(timeInterval: 30.0, target: self, selector: "refresh", userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(self.myTimer, forMode: NSDefaultRunLoopMode)
func refresh() {
// a refresh of the table
}
所以我有一个 table 从我的数据库中获取一些数据并将其显示在他的单元格中。我制作了一个刷新按钮来重新加载我的代码,这样当您点击它时,用户就可以看到有人添加到数据库中的新条目。
这就是我的刷新按钮的代码:
@IBAction func refreshButton(sender: AnyObject) {
textArray.removeAllObjects()
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Conversation", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
publicCloudDatabase?.performQuery(query, inZoneWithID: nil,
completionHandler: ({results, error in
if (error != nil) {
dispatch_async(dispatch_get_main_queue()) {
self.notifyUser("Cloud Access Error",
message: error.localizedDescription)
}
} else {
if results.count > 0 {
var record = results[0] as! CKRecord
self.currentRecord = record
dispatch_async(dispatch_get_main_queue()) {
for x in results{
self.textArray.addObject(x.objectForKey("message") as! String)
self.tableView.reloadData()
}
}
} else {
dispatch_async(dispatch_get_main_queue()) {
self.notifyUser("No Match Found",
message: "No record matching the address was found")
}
}
}
}))
}
我想删除该按钮并每 30 秒刷新一次 table(在应用程序的后台 - 我不希望用户知道它),它那可能吗 ?如果可以,怎么办?
谢谢!
1.performQuery 每 30 秒。
2.compare旧数据与新数据的区别
3.Insert 使用这个 Api
的不同部分- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
而不是重新加载数据
使用计时器是您可以实现的一种方式, 在 ViewDidload
self.myTimer = NSTimer(timeInterval: 30.0, target: self, selector: "refresh", userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(self.myTimer, forMode: NSDefaultRunLoopMode)
func refresh() {
// a refresh of the table
}