Fixes for ImageGridRow

This commit is contained in:
Selim Mustafaev 2020-12-22 01:43:17 +03:00
parent 98bcfc540d
commit 7edc63d06f
2 changed files with 21 additions and 9 deletions

View File

@ -13,6 +13,8 @@ class ImageCell: UICollectionViewCell {
self.imgView = UIImageView() self.imgView = UIImageView()
self.contentView.addSubview(self.imgView) self.contentView.addSubview(self.imgView)
self.imgView.translatesAutoresizingMaskIntoConstraints = false self.imgView.translatesAutoresizingMaskIntoConstraints = false
self.imgView.contentMode = .scaleAspectFill
self.imgView.layer.masksToBounds = true
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
self.imgView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor), self.imgView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor),
self.imgView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor), self.imgView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor),
@ -21,9 +23,18 @@ class ImageCell: UICollectionViewCell {
]) ])
} }
override func prepareForReuse() {
super.prepareForReuse()
self.imgView.kf.cancelDownloadTask()
}
func configure(with image: String) { func configure(with image: String) {
guard let url = URL(string: image) else { return } guard let url = URL(string: image) else { return }
self.imgView.kf.setImage(with: url) self.imgView.kf.setImage(with: url, options: [
.processor(DownsamplingImageProcessor(size: self.imgView.frame.size)),
.scaleFactor(UIScreen.main.scale),
.cacheOriginalImage
])
} }
} }
@ -51,6 +62,7 @@ class ImageGrid: UICollectionView, UICollectionViewDataSource, UICollectionViewD
layout.minimumLineSpacing = spacing layout.minimumLineSpacing = spacing
layout.estimatedItemSize = .zero layout.estimatedItemSize = .zero
self.init(frame: .zero, collectionViewLayout: layout) self.init(frame: .zero, collectionViewLayout: layout)
self.backgroundColor = .clear
self.columnsCount = columns self.columnsCount = columns
self.spacing = spacing self.spacing = spacing
} }
@ -87,8 +99,6 @@ class ImageGrid: UICollectionView, UICollectionViewDataSource, UICollectionViewD
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellSize = (self.bounds.size.width - self.spacing*(CGFloat(self.columnsCount) - 1))/CGFloat(self.columnsCount) let cellSize = (self.bounds.size.width - self.spacing*(CGFloat(self.columnsCount) - 1))/CGFloat(self.columnsCount)
print("====================================")
print("bounds: \(self.bounds)")
return CGSize(width: cellSize, height: cellSize) return CGSize(width: cellSize, height: cellSize)
} }
} }

View File

@ -15,19 +15,21 @@ final class ImageGridCell: Cell<[String]>, CellType {
override func setup() { override func setup() {
super.setup() super.setup()
self.grid = ImageGrid(columns: 3) self.grid = ImageGrid(columns: 3, spacing: 2)
self.grid.translatesAutoresizingMaskIntoConstraints = false self.grid.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(self.grid) self.contentView.addSubview(self.grid)
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
self.grid.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor), self.grid.leadingAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.leadingAnchor),
self.grid.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor), self.grid.trailingAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.trailingAnchor),
self.grid.topAnchor.constraint(equalTo: self.contentView.topAnchor), self.grid.topAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.topAnchor),
self.grid.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor) self.grid.bottomAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.bottomAnchor)
]) ])
} }
override func update() { override func update() {
super.update() super.update()
self.textLabel?.text = nil
self.detailTextLabel?.text = nil
self.grid.set(images: row.value ?? []) self.grid.set(images: row.value ?? [])
} }
} }
@ -37,4 +39,4 @@ final class ImageGridRow: Row<ImageGridCell>, RowType {
super.init(tag: tag) super.init(tag: tag)
cellProvider = CellProvider<ImageGridCell>() cellProvider = CellProvider<ImageGridCell>()
} }
} }