2016-09-24 4 views
1

좋아, 나는이 튜토리얼 (https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2)을 사용하여 tablebase에서 Firebase로 행을 삭제했지만 작동하지 않는 것으로 보인다. 내 코드는 다음과 같이Firebase + 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)

관련 문제