使 CollectionView Cell 水平单元格比其他单元格大 [英] Make CollectionView Cell horizontal cell bigger than others

查看:32
本文介绍了使 CollectionView Cell 水平单元格比其他单元格大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 collectionView,想让中心单元格比其他单元格大,当移动到上一个或下一个单元格时,让它在中心更大

I have a collectionView and want to make the center cell is bigger than other cells and when move to previous or next cell make it bigger in center

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == self.collectionView {
        return slidData.count
    } else {
        return categoryData.count
    }
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == self.collectionView {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! SliderImgCell

        cell.categoryName.text = slidData[indexPath.row].imageTitle
        cell.detail.text = slidData[indexPath.row].imageContent
        cell.pics = slidData[indexPath.item]

        return cell
    } else{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "categoryCell", for: indexPath) as! CategoryCell

        cell.categoryName.text = categoryData[indexPath.row].depName
        cell.pics = categoryData[indexPath.item]

        return cell
    }
}

推荐答案

你需要像这样保存选中的indexPath:

You need to save the selected indexPath like this:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    selectedIndexPath = indexPath 
}

您的集合视图必须确认 UICollectionViewDelegateFlowLayout.然后在 sizeForItemAt 方法中调整您的单元格的大小:

Your collection view must confirm UICollectionViewDelegateFlowLayout. Then in sizeForItemAt method resize your cell:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if indexPath == selectedIndexPath {
        return CGSize(width: 200, height: 90)
    } else {
        return CGSize(width: 180, height: 80)
    }
}

这篇关于使 CollectionView Cell 水平单元格比其他单元格大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆