Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 添加新条目后如何重新加载tableview?_Ios_Swift_Uitableview_Tableview_Cloudkit - Fatal编程技术网

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

Ios 添加新条目后如何重新加载tableview?,ios,swift,uitableview,tableview,cloudkit,Ios,Swift,Uitableview,Tableview,Cloudkit,我正在创建cloudkit表视图。我加载该应用程序,我的tableview将显示我的云工具包条目 然后,我使用add方法insertNewObject,将记录添加到云工具包中,但这不会显示在我的tableview中。它只会在我下一次运行应用程序时显示 func insertNewObject(sender: AnyObject) { let record = CKRecord(recordType: "CloudNote") record.setObject("New N

我正在创建cloudkit表视图。我加载该应用程序,我的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
}
主视图中的My 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调用InsertRowatineXpath:。这将确保您的数据和tableView再次同步,并且至少为您的新数据对象调用tableView:cellForRowAtIndexPath:(以及其他可能的数据对象,因为某些单元格现在可能被重用以显示其他数据)

  • 请注意,顺序始终是:先更新数据,然后更新UI(据我所知,只有在使用UISwitch时,他的UI才会有问题)。

    这个方法是什么,MyClipManager。SaveMethod(Database!,myRecord:record)做什么的???
    重新加载数据
    不会做任何有用的事情,除非更新表视图的数据源。在哪里向
    对象
    数组添加对象?您好,我已经用我的SaveMethod添加了一个更新。它保存到云工具包中的数组中。请看我的答案,可以在这里找到-@RafałAugustyniak这是在目标C中,所以我恐怕无法理解它的头绪。谢谢你的回答。这很有道理。一个问题:我如何找出我的新记录插入到哪个索引,我猜这应该在我的save方法中完成并返回?是的,您可以从save方法返回它(确保它是一个抛出的方法,以便您的数据源知道该方法何时失败,并且不应该在tableView中插入新行)。因此,基本上:新创建的数据对象将以
    对象
    变量结束。如果这是一个可变数组(不要跨不同的线程使用!),那么您可以通过
    insertAtIndex:
    addObject:
    添加新的数据对象。对于第一个,您决定索引,对于最后一个,索引将是添加对象之前的计数。