Ios 如何获取错误“;致命错误:索引超出范围“;从Firestore检索数据时?
我从不同单元格的两个不同集合中检索用户数据,我可以检索数据,但可以将数据设置为Ios 如何获取错误“;致命错误:索引超出范围“;从Firestore检索数据时?,ios,swift,firebase,uitableview,Ios,Swift,Firebase,Uitableview,我从不同单元格的两个不同集合中检索用户数据,我可以检索数据,但可以将数据设置为 override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { retu
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 + posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "detailscell") as! DetailsCellInHomeScreen
if let imageURL = currentUserImageUrl {
cell.configCell(userImgUrl: imageURL)
cell.shareBtn.addTarget(self, action: #selector(toCreatePost), for: .touchUpInside)
}
cell.set(details: details[indexPath.row])
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
cell.btnComment.tag = indexPath.row
cell.btnComment.addTarget(self, action: #selector(toComments(_:)), for: .touchUpInside)
cell.favoritebutton.tag = indexPath.row
cell.favoritebutton.addTarget(self, action: #selector(favupdate(_:)), for: .touchUpInside)
cell.set(post: posts[indexPath.row - 1])
return cell
}
这一行有错误
cell.set(details: details[indexPath.row])
一旦
posts
或details
数组为空,就会发生崩溃
像这样修改您的cellForRow
函数
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 && details.count > 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "detailscell") as! DetailsCellInHomeScreen
if let imageURL = currentUserImageUrl {
cell.configCell(userImgUrl: imageURL)
cell.shareBtn.addTarget(self, action: #selector(toCreatePost), for: .touchUpInside)
}
cell.set(details: details[indexPath.row])
return cell
} else if posts.count > (indexPath.row - 1) {
let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
cell.btnComment.tag = indexPath.row
cell.btnComment.addTarget(self, action: #selector(toComments(_:)), for: .touchUpInside)
cell.favoritebutton.tag = indexPath.row
cell.favoritebutton.addTarget(self, action: #selector(favupdate(_:)), for: .touchUpInside)
cell.set(post: posts[indexPath.row - 1])
return cell
} else {
return UITableViewCell()
}
}
采取预防措施并进行有条件的检查,如:
if details.indices.contains(indexPath.row) {
cell.set(details: details[indexPath.row])
}
if posts.indices.contains(indexPath.row - 1) {
cell.set(post: posts[indexPath.row - 1])
}
您的阵列似乎尚未完成 您的代码的问题是您正在返回
1+个帖子。计数来自numberofrowsin部分:
方法如下
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 + posts.count
}
当在posts.count到达索引路径时,您没有处理
解决方案
因此,解决方案是从numberOfRowsInSection:方法返回posts.count
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
或者您可以在cellForRow:
方法中处理此致命错误
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
.
.
.
//Handle fatal error here
cell.set(details: details[indexPath.row])
return cell
}
.
.
.
//Handle fatal error here
cell.set(post: posts[indexPath.row - 1])
return cell
}
我们能在您的表视图控制器中找到所有与posts
、详细信息
、firestore调用有关的代码吗?这,返回1+posts.count
,这,posts[indexPath.row-1]
,让我相信您在代码或设计中的其他地方做错了什么。请不要再次发布相同的问题。此解决方案在“cell.set(post:posts[indexPath.row-1])处给出了相同的错误。是否尝试设置断点以查看帖子中有多少元素?
?是的,我通过“return posts.count”而不是“return 1+posts.count”解决了此问题