[英]How do I expand a custom collection view cell that has a table view on it?
I am trying to automatically make a collectionView cell expand vertically to show a tableView that is on the cell. 我试图自动使collectionView单元垂直扩展以显示该单元上的tableView。
The tableView is being correctly populated, and I am calling reload on the collection view cell after setting cell.tableView.isHidden = false
tableView已正确填充,在设置
cell.tableView.isHidden = false
后,我在集合视图单元格上调用reload
Swift 4.2, Xcode 10.3 Swift 4.2,Xcode 10.3
Edit: To clarify my question, I am trying to toggle expand/collapse the cell (with auto resize) when I show/hide the tableView on the cell. 编辑:为了澄清我的问题,当我在单元格上显示/隐藏tableView时,我试图切换扩展/折叠单元格(具有自动调整大小)。
collectionView flowLayout var: collectionView flowLayout变量:
// FlowLayout of the collectionView
var flowLayout: UICollectionViewFlowLayout {
return self.collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
}
collectionView viewDidLoad(): collectionView viewDidLoad():
self.flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
collectionView cell: 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 when "show table view" button is tapped: 轻按“显示表格视图”按钮时的collectionView单元格:
cell.tableView.isHidden = false
collectionView.reloadItems(at: [indexPath])
Thanks. 谢谢。
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)
}
Or 要么
@IBAction func showTableView(_ sender: Any) {
flowLayout.itemSize = CGSize(width: newCellWidth, height: newCellHeight)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.