簡體   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