我想基於數組中的項目顯示tableview單元格,但由於一些奇怪的原因,它只會顯示數組中的第一個項目。當我使用打印語句時,它顯示正在迭代的數組。for循環只顯示數組中的第一項當試圖顯示在tableView
這裏是數組:
var restaurants = ["Truckyard", "EasySlider", "Revolver", "Armoury"]
這裏是的cellForRowAtIndexPath:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = restaurantTableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! restaurantCell
for rest in restaurants {
cell.restaurantImageView.image = UIImage(named: rest)
cell.restaurantNameLabel.text = rest
}
return cell
}
你寫錯了。 您在單個單元格中有多少圖片視圖? –
你不在'cellForRowAt'中使用'for'循環。這個函數每行調用一次。使用'indexPath.row'從數組中訪問正確的元素 – Paulw11