【问题标题】:How to reload tableview after adding new entry?添加新条目后如何重新加载tableview?
【发布时间】:2016-04-30 04:46:19
【问题描述】:

我正在创建一个 cloudkit 表格视图。我加载了应用程序,然后我的 tableview 出现了,其中包含我来自云套件的条目。

然后我使用我的添加方法insertNewObject 将记录添加到云工具包,但这不会显示在我的表格视图中。它只会在我下次运行该应用时显示。

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

        }

【问题讨论】:

  • MyClipManager.SaveMethod(Database!, myRecord:record) 这个方法有什么作用??
  • reloadData 不会做任何有用的事情,除非您更新表格视图的数据源。在objects 数组中添加对象的位置在哪里?
  • 您好,我已经用我的 SaveMethod 添加了一个更新。它保存到云套件中的数组中。
  • 看看我的答案,可以在这里找到 - stackoverflow.com/questions/10408232/…
  • @RafałAugustyniak 这是在目标 C 中,所以恐怕我无法做出正面或反面。

标签: ios swift uitableview tableview cloudkit


【解决方案1】:

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

做你想做的事,顺序如下:

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

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

【讨论】:

  • 感谢您的回复。这很有意义。一个问题:我如何找出我的新记录插入到哪个索引,我猜这应该在我的保存方法中完成并返回?
  • 是的,您可以从您的保存方法中返回它(确保它是throws 的方法,以便您的数据源知道该方法何时失败并且不应该在tableView 中插入新行) .所以基本上:您新创建的数据对象将最终出现在您的 objects 变量中。如果这是一个可变数组(不要在不同的线程中使用它!),那么您可以通过insertAtIndex:addObject: 添加新的数据对象。第一个由您决定索引,最后一个索引将是您添加对象之前的计数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多