Не уверен, что эта игровая площадка отражает то, что вы пытаетесь сделать, но покажет вам, как избежать создания collectionView каждый раз:
import UIKit
import PlaygroundSupport
class InnerCell: UICollectionViewCell {
let textLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.backgroundColor = .white
textLabel.layer.borderColor = UIColor.gray.cgColor
textLabel.layer.borderWidth = 1
textLabel.textAlignment = .center
textLabel.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(textLabel)
NSLayoutConstraint.activate([
textLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
textLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
textLabel.topAnchor.constraint(equalTo: contentView.topAnchor),
textLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
])
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
}
class Cell: UICollectionViewCell, UICollectionViewDataSource {
var data: [Int]! = [] {
didSet {
collectionView.reloadData()
}
}
private let collectionView: UICollectionView
override init(frame: CGRect) {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 1
layout.minimumInteritemSpacing = 1
layout.itemSize = .init(width: 220, height: 220)
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
super.init(frame: frame)
collectionView.isPagingEnabled = true
collectionView.backgroundColor = .white
collectionView.register(InnerCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.dataSource = self
collectionView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(collectionView)
NSLayoutConstraint.activate([
collectionView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
collectionView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
collectionView.topAnchor.constraint(equalTo: contentView.topAnchor),
collectionView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
])
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! InnerCell
cell.textLabel.text = String(data[indexPath.item])
return cell
}
}
class VC: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let data = [
0: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
1: [6, 4],
2: [5, 5, 5, 5, 6],
3: [1, 2, 3, 4, 5, 6, 7]
]
override init(collectionViewLayout layout: UICollectionViewLayout) {
super.init(collectionViewLayout: layout)
collectionView.isPagingEnabled = true
collectionView.register(Cell.self, forCellWithReuseIdentifier: "Cell")
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Cell
cell.data = data[indexPath.item]
return cell
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return collectionView.bounds.size
}
}
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
PlaygroundPage.current.liveView = VC(collectionViewLayout: layout)