[英]How do I expand a custom collection view cell that has a table view on it?
我試圖自動使collectionView單元垂直擴展以顯示該單元上的tableView。
tableView已正確填充,在設置cell.tableView.isHidden = false
后,我在集合視圖單元格上調用reload
Swift 4.2,Xcode 10.3
編輯:為了澄清我的問題,當我在單元格上顯示/隱藏tableView時,我試圖切換擴展/折疊單元格(具有自動調整大小)。
collectionView flowLayout變量:
// FlowLayout of the collectionView
var flowLayout: UICollectionViewFlowLayout {
return self.collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
}
collectionView viewDidLoad():
self.flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
collectionView單元格:
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
setNeedsLayout()
layoutIfNeeded()
let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
var frame = layoutAttributes.frame
frame.size.height = ceil(size.height)
layoutAttributes.frame = frame
let autoLayoutAttributes = super.preferredLayoutAttributesFitting(layoutAttributes)
// Specify you want _full width_
let targetSize = CGSize(width: layoutAttributes.frame.width, height: 0)
// Calculate the size (height) using Auto Layout
let autoLayoutSize = contentView.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: UILayoutPriority.required, verticalFittingPriority: UILayoutPriority.defaultLow)
let autoLayoutFrame = CGRect(origin: autoLayoutAttributes.frame.origin, size: autoLayoutSize)
// Assign the new size to the layout attributes
autoLayoutAttributes.frame = autoLayoutFrame
return autoLayoutAttributes
}
輕按“顯示表格視圖”按鈕時的collectionView單元格:
cell.tableView.isHidden = false
collectionView.reloadItems(at: [indexPath])
謝謝。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cell = collectionView.cellForItem(at: indexPath) as! yourCell
if cell.tableView.isHidden {
return CGize(width: cellWidth, height:0)
}
return CGSize(width: tableViewWidth, height: TotalTableViewCellsHeight)
}
要么
@IBAction func showTableView(_ sender: Any) {
flowLayout.itemSize = CGSize(width: newCellWidth, height: newCellHeight)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.