【发布时间】:2021-06-28 00:59:14
【问题描述】:
我对 Swift 还很陌生,有点困惑。我已经对我的 Firestore 进行了编程,以将数据加载到 TableView 中。但是,当我在 tableView 中滚动加载数据时,tableView 中的单元格消失了。我已经复制了下面的逻辑代码,想知道是否有人知道为什么代码单元会消失?
我看到其他人问过这个问题,但当我使用他们的建议时运气不佳。谢谢!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexPath.row > myRatingsRestaurant.count-1){
return UITableViewCell()
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyRatingsViewCell", for: indexPath) as! MyRatingsViewCell
cell.tag = indexPath.row
let myRatingRestaurant = myRatingsRestaurant[indexPath.row] //Thread 1: Fatal error: Index out of range
cell.set(myRatingRestaurant: myRatingRestaurant)
return cell
}
}
【问题讨论】:
-
'return UITableViewCell()' 是什么意思?
-
@ElTomato 是在返回 Cells 时处理
default的情况,而不是抛出异常,因为UITableViewCell是所有单元格的基础。这是 iOS 开发中非常常见的做法 :) 谢谢。