I am trying to automatically make a collectionView cell expand vertically to show a tableView that is on the cell.
The tableView is being correctly populated, and I am calling reload on the collection view cell after setting cell.tableView.isHidden = false
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.
collectionView flowLayout var:
// FlowLayout of the collectionView
var flowLayout: UICollectionViewFlowLayout {
return self.collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
}
collectionView viewDidLoad():
self.flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
collectionView cell:
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:
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)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.