пробывал уже, не получилось, может что ни так в классе ячейки таблицы где я создаю колекшенвью? можете посмотреть?
import UIKit
class PropertyComparisonTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var values: UICollectionView!
let width: CGFloat = 100
var maxHeight: CGFloat = 0
let indents: CGFloat = 30
var rowIndex: Int!
var propertyComparison: PropertyComparison! {
didSet {
values.dataSource = self
// define height of cells
defineHeightWith(text: propertyComparison.name)
for value in propertyComparison.values {
defineHeightWith(text: value)
}
values.heightAnchor.constraint(equalToConstant: maxHeight + indents).isActive = true
let cellSize = CGSize(width: width + indents, height: maxHeight + indents)
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.itemSize = cellSize
layout.minimumLineSpacing = 4
layout.headerReferenceSize = cellSize
layout.sectionHeadersPinToVisibleBounds = true
layout.sectionInset = UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 0)
values.setCollectionViewLayout(layout, animated: false)
values.reloadData()
}
}
private func defineHeightWith(text: String) {
let fontStyle: UIFont = UIFont(name: "PTSans-Regular", size: 13) ?? UIFont.systemFont(ofSize: 13)
let height = text.heightWithConstrainedWidth(width: width, font: fontStyle)
if height > maxHeight {
maxHeight = height
}
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath) as? PropertyHeaderComparisonCollectionReusableView {
header.index = rowIndex
header.label.text = propertyComparison.name
return header
}
return UICollectionReusableView(frame: .zero)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return propertyComparison.values.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "value", for: indexPath) as? ValueComparisonCollectionViewCell {
cell.index = rowIndex
cell.label.text = propertyComparison.values[indexPath.row]
return cell
}
return UICollectionViewCell(frame: .zero)
}
}