Skip to content

Instantly share code, notes, and snippets.

@rr-codes
Created August 6, 2021 05:04
Show Gist options
  • Save rr-codes/0a0f4632c968b0b3d17ef700ff430e70 to your computer and use it in GitHub Desktop.
Save rr-codes/0a0f4632c968b0b3d17ef700ff430e70 to your computer and use it in GitHub Desktop.
class FlagListTableViewCell: UITableViewCell, ReusableView {
private let flagImageView = UIImageView()
private var cellHeightConstraint: NSLayoutConstraint!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.cellHeightConstraint = self.flagImageView.heightAnchor.constraint(equalToConstant: 0)
self.cellHeightConstraint.isActive = true
self.flagImageView.contentMode = .scaleAspectFit
self.flagImageView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(self.flagImageView)
self.flagImageView.pinEdges(to: self.contentView)
}
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
let imageViewWidth = self.contentView.frame.width
let imageWidth = CGFloat(self.flagImageView.image!.cgImage!.width)
let imageHeight = CGFloat(self.flagImageView.image!.cgImage!.height)
let scaledHeight = imageHeight * (imageViewWidth / imageWidth)
self.cellHeightConstraint.constant = scaledHeight
}
func bind(to country: Country) {
let image = UIImage(named: country.flagImageName)!
self.flagImageView.image = image
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment