简体   繁体   English

Firebase + Swift 删除 TableView 中的行

[英]Firebase + Swift Deleting Rows in TableView

Ok, I was following this tutorial ( https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2 ) for deleting rows in tableView with Firebase but it just doesn't seem to work.好的,我正在按照本教程( https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2 )使用 Firebase 删除 tableView 中的行,但它似乎不起作用。 My code as follows:我的代码如下:

override func viewDidLoad() {
    super.viewDidLoad()
    configureDatabase() 
}

func configureDatabase() {
    self.ref = FIRDatabase.database().reference()
    if let user = FIRAuth.auth()?.currentUser {
        self.ref.child("mileage").queryOrderedByChild("user").queryEqualToValue(user.uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in
            self.mileageDatabase.insert(snapshot, atIndex: 0)
            self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Automatic)

        })


    } else {

    }

}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
        let row = self.mileageDatabase[indexPath.row]
        row.ref.removeValue()
        self.tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: indexPath.row, inSection: 0)], withRowAnimation: .Automatic)

    }
}

It throws the error:它抛出错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (8) must be equal to the number of rows contained in that section before the update (8), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (8) 必须等于行数更新前包含在该部分中 (8),加上或减去从该部分插入或删除的行数(0 插入,1 删除),加上或减去移入或移出该部分的行数(0 移入) , 0 移出).'

I think that here row.ref.removeValue() there is a mistake.我认为这里row.ref.removeValue()有一个错误。 You are mixing your Array with Firebase data.您正在将 Array 与 Firebase 数据混合。 If you write just:如果你只写:

mileageDatabase.remove[indexPath.row] tableview.reloadData()

For me is working this way.对我来说就是这样工作。 You have to capture the userkey for that row.您必须捕获该行的用户密钥。 I have an array like yours (mileageDatabase = teste) but my variables are stored at an struct inside a sublcass (ChkList).我有一个像你这样的数组(mileageDatabase = teste),但我的变量存储在子类(ChkList)内的结构中。 That is the way i did it.我就是这样做的。

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

if editingStyle == .delete {

        let user: ChkList = teste[indexPath.row]
        let uk = user.key
        print("Userkey: \(uk)")
        ref.child(uk!).removeValue()
}
}

This way you will have removed the data from Firebase and the row form tableview.通过这种方式,您将从 Firebase 和行表单 tableview 中删除数据。 You can see my ask where i could figure out how to fix this at: Firebase: delete item from row (Swift 3)你可以看到我的问题,我在哪里可以弄清楚如何解决这个问题: Firebase: delete item from row (Swift 3)

The data source still hold that object whose cell you deleted .数据源仍然保存您删除其单元格的对象。 You have to remove that object from the data source, as you delete the cell.在删除单元格时,您必须从数据源中删除该对象。

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

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