2016-11-10 6 views
1

テーブルビュー付きのuiviewcontrollerがあります。イムは、私のfirebaseデータベースのユーザーによる照会しようとしているが、私はこのエラーを取得しておいてください。viewdidloadからテーブルビューに行を挿入

'NSInternalInconsistencyException', reason: 'attempt to insert row 1 into section 0, but there are only 0 rows in section 0 after the update'

私はそれを見て、beginUpdatesとendUpdates方法を試してみましたが、私はendUpdatesを呼び出す行に同じエラーを取得します。なぜこのようなことが起こっているのでしょうか?

はHERESに私のコード:問題に関するコメントで説明したように

searchController.searchResultsUpdater = self 
searchController.dimsBackgroundDuringPresentation = false 
searchController.definesPresentationContext = true 
definesPresentationContext = true 
self.browseTable.tableHeaderView = searchController.searchBar 



    ref.child("users").queryOrdered(byChild: "name").observe(.childAdded, with: { (snapshot) in 

     self.usersArray.append(snapshot.value as? NSDictionary) 

     //insert rows 
     //self.browseTable.beginUpdates() 

     //where the error occurs 
     self.browseTable.insertRows(at: [IndexPath(row:self.usersArray.count - 1,section: 0)], with: .automatic) 

     //self.browseTable.endUpdates() 


}) { (error) in 
    print("ERROR: \(error.localizedDescription)") 

} 
+0

を、あなたは' self.usersArray.count'または任意のハードコードされた数を返すのですか? – KrishnaCA

+0

@KrishnaChaitanyaAmjuriこれはあなたが新しい挿入しようとしている場合は 'searchController.isActive && searchController.searchBar.textあれば!= "" { リターンfilteredUsers.count } リターン1' 私のnumberOfRowsInSection方法 – user3462448

+0

には何かということです行。また、 'numberOfRows'メソッドで返された値を、挿入後の新しい行数に更新する必要があります。あなたのメソッドを見て、あなたが 'indexPath.row = self.usersArray.count - 1'に行を挿入していることを考えれば、これはあなたのクラッシュを引き起こすものだと思います。 – KrishnaCA

答えて

1

、クラッシュが原因の行を挿入した後の行数と一致しない機能func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Intで返される値に起因します。

numberOfRowsInSectionで返された値がdataSourceオブジェクトに基づいている場合は、行を挿入/削除する前にdataSourceオブジェクトを更新すると問題が解決されます。

が編集を提案するお気軽に:)あなた `numberOfRowsInSection`方法で

関連する問題