简体   繁体   English

添加新条目后如何重新加载tableview?

[英]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: 要做你想做的,这是订单:

    • Insert a new data object into your datasource (self.objects). 将新数据对象插入数据源(self.objects)。 Make sure you get the index of where it ends up in the array. 确保获得它在数组中结束的索引。
    • Call insertRowAtIndexPath: with the correct indexPath on your tableView. 使用tableView上正确的indexPath调用insertRowAtIndexPath:。 This will make sure your data and tableView are in sync again, and tableView:cellForRowAtIndexPath: is called for at least your new data object (and possible others, as certain cells might now be reused to display other data). 这将确保您的数据和tableView再次同步,并且至少为您的新数据对象调用tableView:cellForRowAtIndexPath:(以及可能的其他数据对象,因为某些单元格现在可能会被重用以显示其他数据)。

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.

相关问题 关闭 UIViewController 后如何重新加载 tableView - How to reload tableView after dismissing a UIViewController 将新数据重新加载到tableView中 - Reload new data into tableView 启动应用程序后,如果每次数组中都出现新内容并且数组相同,则如何重新加载tableview,则无需重新加载 - after launching app How to reload tableview if any new content appears in array every time and if array is same then doesn’t need to reload 删除、添加或修改 Firestore 文档和分页结果后重新加载 TableView - Reload TableView After Deleting, Adding, or Modifying Firestore Document and Paginating Results UITableView 添加新条目后不会刷新 - UITableView wont refresh after adding a new entry 删除后重新加载应用程序时,TableView中的CoreData条目重新出现 - My CoreData entry in TableView reappears when I reload the app after deletion 如何自动重新加载tableview - how to reload tableview automatically 回到tableview后Tableview重新加载行 - Tableview reload row after come back tableview ios / coredata / nfetchresultscontroller。 添加新记录后,如何获取记录的表视图以进行更新 - ios/coredata/nfetchresultscontroller. How do I get a tableview of records to update after adding a new one 在插入行后重新加载tableView - reload tableView after inserting row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM