我有一个始终带有一个单元格的表格视图。如果有要从Firebase下载的数据,它将把它放在一个称为posts的数组中。例如,当用户要下载两台“我为例”的服务器时,它将仅显示一个单元格,而不显示两个单元格。我认为我可以通过将return posts.count
更改为return posts.count + 1
来解决此问题,因为将始终显示一个单元格。但是,如果我使用return posts.count + 1
,我会得到一个
第线程1:致命错误:索引超出范围
let post = posts[indexPath.row]
行的错误。我已经阅读了有关此错误的信息,但似乎无法修复。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if posts.count == 0 {
self.tableView.setEmptyMessage("No Servers uploaded!")
return 1
} else {
self.tableView.restore()
return posts.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell", for: indexPath) as! ProfileCellTableViewCell
cell.delegate = self
return cell
}else {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCellMYposts
cell.Map_image.image = nil
let post = posts[indexPath.row]
cell.post = post
cell.delegate = self
return cell
}
}
答案 0 :(得分:0)
假设您在posts[0]
中有一些数据,那么您实际上就不会显示它。对于indexPath.row = 0,将显示一个配置文件单元格,然后开始显示posts [1]及以后的数据。将您的问题行更改为:
let post = posts[indexPath.row - 1]