I'm trying to implement UIcollectionView inside UITableViewCell. I've tried several methods but none of them works for me. Looks like tableView just doesn't know which size cell should be.
import UIKit
class MovieVideosTableViewCell: UITableViewCell {
static let identifier = "MovieVideosTableViewCell"
private var collectionView: UICollectionView! = nil
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
print("Inited \(type(of: self))")
setupCollectionView()
addSubview(collectionView)
setupConstraints()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
private extension MovieVideosTableViewCell {
func setupCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.itemSize = CGSize(width: contentView.bounds.width/2, height: contentView.bounds.height)
collectionView = UICollectionView(frame: contentView.bounds, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.register(MovieDetailsCollectionViewCell.self, forCellWithReuseIdentifier: MovieDetailsCollectionViewCell.identifier)
collectionView.delegate = self
collectionView.dataSource = self
}
func setupConstraints() {
NSLayoutConstraint.activate([
collectionView.topAnchor.constraint(equalTo: contentView.topAnchor),
collectionView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
collectionView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
collectionView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor)
])
}
}
CodePudding user response:
So, the actual problem was in constraints. I used collectionView.leadingAnchor.constraint
twice, instead of collectionView.trailingAnchor.constraint
CodePudding user response:
I don't know what you are trying to do, but maybe the new Compositional Layout for collectionView would be useful for you. You can create multiples sections with different configuration for each cell.
You can check it out here -> https://developer.apple.com/documentation/uikit/uicollectionviewcompositionallayout