繁体   English   中英

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

[英]How to reload tableview after adding new entry?

我正在创建一个cloudkit tableview。 我加载应用程序,我的tableview显示我的云套件条目。

然后我使用我的add方法insertNewObject将记录添加到云套件中,但这不会显示在我的tableview中。 它只会出现在我下次运行的应用程序中。

   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()
    }


    }

这是我的添加方法。 我正在调用tableview重载,你可以看到,但什么也没发生。

我的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
}

根据要求:保存到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
}

我在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()
            }

        }

插入单个对象时,不应重新加载整个tableView。 只有当您知道所有数据都已更改时才这样做。

要做你想做的,这是订单:

    • 将新数据对象插入数据源(self.objects)。 确保获得它在数组中结束的索引。
    • 使用tableView上正确的indexPath调用insertRowAtIndexPath:。 这将确保您的数据和tableView再次同步,并且至少为您的新数据对象调用tableView:cellForRowAtIndexPath:(以及可能的其他数据对象,因为某些单元格现在可能会被重用以显示其他数据)。

请注意,顺序始终是:首先更新您的数据,然后更新您的UI(我知道他使用UISwitch时只有毛茸茸的地方)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM