Ios 从viewdidload将行插入表格视图
我有一个uiviewcontroller,里面有一个tableview。我试图通过我的firebase数据库中的用户进行查询,但不断出现以下错误: “NSInternalInconsistencyException”,原因:“尝试将行1插入节0,但更新后节0中只有0行” 我查了一下,尝试了BeginUpdate和endUpdates方法,但在我称为endUpdates的行中得到了相同的错误。知道为什么会这样吗 这是我的密码:Ios 从viewdidload将行插入表格视图,ios,uitableview,firebase,firebase-realtime-database,swift3,Ios,Uitableview,Firebase,Firebase Realtime Database,Swift3,我有一个uiviewcontroller,里面有一个tableview。我试图通过我的firebase数据库中的用户进行查询,但不断出现以下错误: “NSInternalInconsistencyException”,原因:“尝试将行1插入节0,但更新后节0中只有0行” 我查了一下,尝试了BeginUpdate和endUpdates方法,但在我称为endUpdates的行中得到了相同的错误。知道为什么会这样吗 这是我的密码: searchController.searchResultsUpdat
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)")
}
如有关问题的注释中所述,导致崩溃的原因是函数
func tableView(u tableView:UITableView,numberofrowsinssection:Int)->Int
中返回的值与插入行后的行数不匹配
在这种情况下,numberofrowsinssection
函数返回的值基于数据源对象,在插入/删除行之前更新数据源对象将解决问题
请随意建议编辑:)在您的
numberofrowsinssection
方法中,您是否返回了self.usersArray.count
或任何硬编码的数字?@KrishnaChaitanyaAmjuri这是我的numberofrowsinssection方法中的内容“if searchController.isActive&&searchController.searchBar.text!”如果要插入新行,“{return filteredUsers.count}返回1”。您还应该在插入后将numberOfRows
方法中返回的值更新为新的行数。通过查看您的方法,并假设您在indepath.row=self.usersArray.count-1插入行,我认为这就是导致crashokay的原因,我想我明白了。谢谢大家!@克里希那恰丹也因为同样的原因吗?如果是的话,我会把它写下来作为一个答案,这样对其他人会有帮助。请让我知道