【发布时间】:2019-01-22 21:48:00
【问题描述】:
我正在使用自定义单元格填充 tableView。我从元组数组(String:String) 中获取单元格标签文本和 imageView 图像,其中第一个值是商店名称,第二个值是存储在 firebase 存储中的图像的 URL。
在我对 firebase 的查询结束时,我得到了 availableShopsArray 填充,并为 tableView 重新加载数据。在cellForRowAt函数中,我将元组的两个参数分配给单元格标签和图像。但在重新加载数据时,我在 AppDelegate 中收到错误。
这是我声明函数的方式:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "shopCell", for: indexPath as IndexPath) as! ShopTableViewCell
cell.shopNameLabel.text = self.availableShopsArray[indexPath.row].0
let url = URL(string: self.availableShopsArray[indexPath.row].1)
let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
cell.imageView?.image = UIImage(data:data!)
return cell
}
这是我的单元格:
import UIKit
class ShopTableViewCell: UITableViewCell {
@IBOutlet weak var makerImageView: UIImageView!
@IBOutlet weak var shopImageView: UIImageView!
@IBOutlet weak var shopNameLabel: UILabel!
@IBOutlet weak var shopRatingLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
任何想法
【问题讨论】:
-
显示如何在 AppDelegate 中重新加载,这也是 rootVC 吗?
-
标签: ios swift uitableview