我有一个问题:我UITableView
用UITableViewCell
,在小区我有UICollectionView
一个不同的数字UICollectionViewCell
。我要UITableViewCell
高度对应于的高度UICollectionView
。我UICollectionView
在这里看到实际的高度:
class TimeViewCell: UITableViewCell {
@IBOutlet weak var labelTime: UILabel!
var dataForShow = [String]() {
didSet{
self.collectionView?.reloadData()
}
}
@IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
collectionView.delegate = self
collectionView.dataSource = self
}
}
extension TimeViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataForShow.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CustomCollectionCell
cell.labelCollection.text = dataForShow[indexPath.row]
let realValueHeightOfCollection = collectionView.contentSize.height
//when print realValueHeightOfCollection I see 50.0 (if dataForShow.count <4) or 110.0 (if dataForShow.count >5)
return cell
}
}
但是如何将那个高度传递给高度UITableViewCell
呢?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellTime", for: indexPath) as! TimeViewCell
let showHour = self.infoWripper.sorted(by: { $0.hour < $1.hour })[indexPath.row]
cell.labelTime.text = showHour.showHour()
cell.dataForShow = showHour.showFullTime()
//here may be set the height of cell?
return cell
}
我通过添加一行来确定我的问题:
tableTime.rowHeight = cell.collectionView.collectionViewLayout.collectionViewContentSize.height
在 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {...}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句