Reputation: 131
I'm attempting to use a custom cell class (customCell) to show images from my array in a cell. If I use the default cell (without 'as! customCell') the images show - so I can only assume it is something to do with my custom cell class (customCell)
Here is my custom cell class code:
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")
}
}
and my tableView code where I'm attempting to call the image (if it matters):
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
}
Upvotes: 0
Views: 1140
Reputation: 8106
You need to set the frame of your 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
}
Upvotes: 1