你可以这样做
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
let width = collectionView.bounds.width
let height = 0.0
// for specific cell height
if indexPath.row == 1{
height = 200.0 // chnage height for a specific cell
}else{
height = collectionView.bounds.height //actual height
}
return CGSize(width: width, height: width)
}
更新
我不确定单击按钮时它是否有效。
但是您可以通过实现didSelectItemAt 来更改选定的特定单元格的高度,如下所示。
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.performBatchUpdates(nil, completion: nil)
// to reload specific cell use below line.
//collectionView.reloadItemsAtIndexPaths([indexPath])
}
您可以检查sizeForItemAt中的idexPath是否被选中。如果它被选中,那么您可以更改高度,否则返回原始高度。像下面这样。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
switch collectionView.indexPathsForSelectedItems?.first {
case .some(indexPath):
let height = 200.0 // here you can change the height for selected cell.
return CGSize(width: view.frame.width,height: height)
default:
let height = 200.0 // standard height
return CGSize(width: view.frame.width, height: height)
}
}