2016-09-24 32 views
1

[OK]を、私はこのチュートリアル(https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2)テーブルベースの行をFirebaseで削除するために行っていましたが、動作していないようです。私のコードは次のように:Firebase + Swift TableViewでの行の削除

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) 

    } 
} 

それはエラーがスローされます:

*** 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).'

答えて

0

データソースはまだそのセル削除し、そのオブジェクトを保持します。セルを削除すると、そのオブジェクトをデータソースから削除する必要があります。

+0

どうやってやっていますか? – Learn2Code

1

こちらはrow.ref.removeValue()と思われますが間違いがあります。 ArrayをFirebaseデータと混合しています。あなただけ書いた場合:私にとって

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

はこのように取り組んでいます。その行のユーザーキーを取得する必要があります。 私はあなたのような配列を持っていますが(mileageDatabase = teste)、変数はサブクラス(ChkList)内の構造体に格納されます。それが私のやり方です。

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

この方法で、Firebaseと行フォームtableviewからデータを削除します。あなたは私がこの問題を解決する方法をどこで見つけることができるかを私の尋ねることができます。Firebase: delete item from row (Swift 3)

関連する問題