[英]How to reload tableview after adding new entry?
I am creating a cloudkit tableview. 我正在创建一个cloudkit tableview。 I load the app and my tableview appears with my entries from cloud kit. 我加载应用程序,我的tableview显示我的云套件条目。
I then use my add method insertNewObject
which adds the record to cloud kit but this does not show up in my tableview. 然后我使用我的add方法insertNewObject
将记录添加到云套件中,但这不会显示在我的tableview中。 It will only show up on my next run of the app. 它只会出现在我下次运行的应用程序中。
func insertNewObject(sender: AnyObject) {
let record = CKRecord(recordType: "CloudNote")
record.setObject("New Note", forKey: "Notes")
MyClipManager.SaveMethod(Database!, myRecord:record)
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
This is my add method. 这是我的添加方法。 I am calling tableview reload as you can see but nothing is happening. 我正在调用tableview重载,你可以看到,但什么也没发生。
My tableview creation code: 我的tableview创建代码:
// Tableview stuff --- Done
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
/////// Get number of rows
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}
//// FIll the table
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let object = objects[indexPath.row]
cell.textLabel!.text = object.objectForKey("Notes") as? String
return cell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
As requested: Method that saves to CloudDB 根据要求:保存到CloudDB的方法
func SaveMethod(publicDatabase: CKDatabase, myRecord: CKRecord ) -> CKRecord {
publicDatabase.saveRecord(myRecord, completionHandler:
({returnRecord, error in
if let err = error {
self.notifyUser("Save Error", message:
err.localizedDescription)
} else {
dispatch_async(dispatch_get_main_queue()) {
self.notifyUser("Success",
message: "Record saved successfully")
}
}
}))
return myRecord
}
My viewdidload method in masterview: 我在masterview中的viewdidload方法:
override func viewDidLoad() {
super.viewDidLoad()
// Database loading on runtime
Database = container.privateCloudDatabase
///Build Query
let query = CKQuery(recordType: "CloudNote", predicate: NSPredicate(format: "TRUEPREDICATE"))
///Perform query on DB
Database!.performQuery(query, inZoneWithID: nil) { (records, error) -> Void in
if (error != nil) {
NSLog("Error performing query. \(error.debugDescription)")
return
}
self.objects = records!
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
You should not reload your entire tableView when you insert a single object. 插入单个对象时,不应重新加载整个tableView。 Only do that when you know ALL the data has changed. 只有当您知道所有数据都已更改时才这样做。
To do what you want, this is the order: 要做你想做的,这是订单:
Note that the order is always: update your data first, then update your UI (the only place I know of that his is hairy is when using a UISwitch). 请注意,顺序始终是:首先更新您的数据,然后更新您的UI(我知道他使用UISwitch时只有毛茸茸的地方)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.