首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扩展UICollectionViewCell on tap

扩展UICollectionViewCell on tap
EN

Stack Overflow用户
提问于 2019-04-11 03:21:00
回答 1查看 189关注 0票数 0

我有垂直向上滚动的UICollectionView,我想在点击中更改项目高度。在默认状态下,单元格高度为2/3屏幕高度。我尝试了以下几点:

代码语言:javascript
复制
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)
    }

但它不起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-11 04:33:12

您可以通过UICollectionViewDelegateFlowLayout方法sizeForItemAt实现这一点

声明一个变量

代码语言:javascript
复制
var selectIndex: Int = -1

在您的didSelectItemAt中添加以下内容

代码语言:javascript
复制
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    self.selectIndex = indexPath.row

    collectionView.reloadData()
}

UICollectionViewDelegateFlowLayout添加扩展

代码语言:javascript
复制
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)
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55619885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档