Из того кода, что написан, максимально приближенный рабочий вариант будет выглядеть следующим образом. Чтобы хорошо разобраться в теме рекомендую посмотреть этот
курс по Collection View.
import UIKit
import AVKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
let imagesCellId = "ImageCellId"
let albumCellId = "AlbumCellId"
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 16
layout.scrollDirection = .vertical
layout.itemSize = CGSize(width: 50, height: 50)
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .clear
return cv
}()
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
func setupViews() {
collectionView.register(VideoCell.self, forCellWithReuseIdentifier: imagesCellId)
collectionView.register(PreviewCell.self, forCellWithReuseIdentifier: albumCellId)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.allowsSelection = true
collectionView.isUserInteractionEnabled = true
view.addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 5).isActive = true
collectionView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
collectionView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: albumCellId, for: indexPath) as! PreviewCell
cell.backgroundColor = UIColor.red
return cell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: imagesCellId, for: indexPath) as! VideoCell
cell.backgroundColor = UIColor.green
return cell
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// Вот тут совсем не понятно, что хотел сказать автор
// collectionView.selectItem(at: IndexPath(item: 0, section: 0), animated: true, scrollPosition: .left)
// collectionView(collectionView, didSelectItemAt: IndexPath(item: 0, section: 0))
print("Выбрана ячейка: (\(indexPath.section), \(indexPath.item))")
}
}
class VideoCell: UICollectionViewCell, AVPlayerViewControllerDelegate {
var videoPlayerController = AVPlayerViewController()
}
class PreviewCell: UICollectionViewCell {
}