【发布时间】:2016-11-10 16:28:05
【问题描述】:
我有一个带有 tableview 的 uiviewcontroller。我试图通过我的 firebase 数据库中的用户进行查询,但我不断收到此错误:
'NSInternalInconsistencyException',原因:'尝试将第1行插入第0节,但更新后第0节只有0行'
我查找并尝试了 beginUpdates 和 endUpdates 方法,但在我调用 endUpdates 的行上得到了相同的错误。任何想法为什么会这样?
这是我的代码:
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)")
}
【问题讨论】:
-
在您的
numberOfRowsInSection方法中,您是否返回了self.usersArray.count或任何硬编码的数字? -
@KrishnaChaitanyaAmjuri 这是我的 numberOfRowsInSection 方法中的内容 'if searchController.isActive && searchController.searchBar.text != "" { return filteredUsers.count } return 1'
-
如果您要插入新行。您还应该将
numberOfRows方法中返回的值更新为插入后的新行数。通过查看您的方法并考虑到您在indexPath.row = self.usersArray.count - 1处插入行,我认为这就是导致您崩溃的原因 -
好吧,我想我明白了。谢谢你! @KrishnaChaitanyaAmjuri
-
是不是同一个原因?如果是,那么我会将其写为答案,以便对其他人有所帮助。请告诉我
标签: ios uitableview firebase firebase-realtime-database swift3