iOS-图像未显示自定义单元格(Swift)

问题描述 投票:0回答:1

我正在尝试使用自定义单元类(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
}
ios swift image firebase custom-cell
1个回答
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
}
© www.soinside.com 2019 - 2024. All rights reserved.