I’m following a tutorial for a messaging chat app and I keep getting errors on the UIImageViews. For example ~ userImageView.right, userImageView.width, etc. all get errors saying UIImageView has no member. I’ve used the right/width/height in other parts of my app, I can’t figure out why it’s not happy here… thoughts?
class ConversationTableViewCell: UITableViewCell {
static let identifier = "ConversationTableViewCell"
private let userImageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = 50
imageView.layer.masksToBounds = true
return imageView
}()
private let userNameLabel: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 21, weight: .semibold)
return label
}()
private let userMessageLabel: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 19, weight: .regular)
label.numberOfLines = 0
return label
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(userImageView)
contentView.addSubview(userNameLabel)
contentView.addSubview(userMessageLabel)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
userImageView.frame = CGRect(x: 10,
y: 10,
width: 100,
height: 100)
userNameLabel.frame = CGRect(x: userImageView.right + 10,
y: 10,
width: contentView.width - 20 - userImageView.width,
height: (contentView.height-20)/2)
userMessageLabel.frame = CGRect(x: userImageView.right + 10,
y: userNameLabel.bottom + 10,
width: contentView.width - 20 - userImageView.width,
height: (contentView.height-20)/2)
}
1
“I’ve used the right/width/height in other parts of my app” Really? Well, they are not built-in Swift / Cocoa UIImageView properties, so either you are lying about that or else you imported some sort of third party library, or used an extension on UIView or UIImageView, that is not in scope here. Do this: find some code where this works, find where it says
right
, and Command-click on it. That will take you to where this is defined. That is what you need to be importing / seeing here as well.