我正在尝试使用自定义单元格类(customCell)来显示单元格中数组中的图像。如果我使用默认单元格(不带“ as!customCell”),那么图像将显示-因此,我只能假定这与我的自定义单元格类(customCell)有关
这是我的自定义单元格类代码:
class customCell: UITableViewCell {
override func layoutSubviews() {
super.layoutSubviews()
}
var customImageView: UIImageView {
let customImage = UIImageView()
customImage.image = UIImage(named: "PlaceholderImage")
return customImage
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
addSubview(customImageView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
和我试图调用图像的tableView代码(如果有的话):
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! customCell
let array = displayArray[indexPath.section]
cell.customImageView.image = UIImage(named: "PlaceholderImage")
if let imageUrl = array.imageUrl {
let url = URL(string: imageUrl)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async {
cell.customImageView.image = UIImage(data: data!)
}
}.resume()
}
return cell
}
答案 0 :(得分:1)
您需要设置UIImageView
的框架:
var customImageView: UIImageView {
let customImage = UIImageView()
customImage.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
customImage.image = UIImage(named: "PlaceholderImage")
return customImage
}