我有垂直向上滚动的UICollectionView,我想在点击中更改项目高度。在默认状态下,单元格高度为2/3屏幕高度。我尝试了以下几点:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
if let cell = cell as? TutorialCell {
let attribute = layout.layoutAttributesForItem(at: indexPath)
attribute?.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width,
height: UIScreen.main.bounds.size.height)
collectionView.reloadItems(at: [indexPath])
}
print(indexPath.row)
}但它不起作用。
发布于 2019-04-11 04:33:12
您可以通过UICollectionViewDelegateFlowLayout方法sizeForItemAt实现这一点
声明一个变量
var selectIndex: Int = -1在您的didSelectItemAt中添加以下内容
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.selectIndex = indexPath.row
collectionView.reloadData()
}为UICollectionViewDelegateFlowLayout添加扩展
extension YourViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = (self.selectIndex == indexPath.row ) ?
UIScreen.main.bounds.size.height :
(UIScreen.main.bounds.size.height * 2) / 3
return CGSize(width:UIScreen.main.bounds.size.width, height:height)
}
}https://stackoverflow.com/questions/55619885
复制相似问题