iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 使用UICollectionView具有不同单元大小的部分大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我必须在 Swift项目中创建一个带有两个不同部分的视图.
一个有七个方形的细胞.第二个有三个矩形的单元格.单元格内的数据是静态的.
在所有情况下,这些部分应该适合整个屏幕宽度(主要是横向模式).

我没有太多使用UICollectionViewController的经验,所以我决定使用一个UICollectionViewController类和两个可重用单元来完成此操作,但是当我设置第一个单元格宽度时,第二个单元格也受到影响,然后单元格宽度被剪切.

我的观点是否正确?
@R_27_10675@用不同的UICollectionViewControllers(每个部分一个)?

更新:我的控制器代码

import UIKit

class InverterController: UICollectionViewController,UICollectionViewDelegateFlowLayout {

    let moduleCell = "moduleCell"
    let parameterCell = "parameterCell"

    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,referenceSizeForHeaderInSection section: int) -> CGSize {
        if section > 0 {
            return CGSizeZero
        } else {
            return CGSize(width: collectionView.frame.width,height: CGFloat(195))
        }
    }

    func collectionView(collectionView: UICollectionView,sizeForItemAtPath indexPath: NSIndexPath) -> CGSize {

        var newSize = CGSizeZero

        if indexPath.section == 0 {

            newSize =  CGSizeMake(CGFloat(105),CGFloat(105))
        } else {
            newSize = CGSizeMake(CGFloat(313),CGFloat(200))
        }

        return newSize
    }

    override func collectionView(collectionView: UICollectionView,viewForSupplementaryElementOfKind kind: String,aTindexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        switch kind {
            case UICollectionElementKindSectionHeader:
                let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind,withReusEIDentifier: "inverterHeader",forIndexPath: indexPath)
                return headerView
            default:
                assert(false,"Unexpected element kind")
        }
    }

    /**
     Two sections in this UICollectionView: First one for clock items,second one for other parameters.
     */
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 2
    }

    /**
     First section contains one cell
     Second section contains three cells
     */
    override func collectionView(collectionView: UICollectionView,numberOfItemsInSection section: int) -> Int {
        var items = 0

        if section == 0 {
            items = 7
        } else {
            items = 3
        }

        return items
    }

    override func collectionView(collectionView: UICollectionView,cellForItemATindexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        var cellIdentifier = ""

        if indexPath.section == 0 {
            cellIdentifier = moduleCell
        } else {
            cellIdentifier = parameterCell
        }

        let cell = collectionView.dequeueReusableCellWithReusEIDentifier(cellIdentifier,forIndexPath: indexPath)
        cell.layer.cornerRadius = 6
        cell.layer.masksToBounds = true

        return cell
    }
}

CollectionView的Interface Builder配置

ios – 使用UICollectionView具有不同单元大小的部分

模拟器截图(非期望的结果;所有单元格具有相同的宽度)

ios – 使用UICollectionView具有不同单元大小的部分

解决方法

您可以使用COllectionView(collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemATindexPath indexPath:NSIndexPath) – > CGSize委托方法来调整单元格大小.

你的情况下你可以写这样的东西:

func collectionView(collectionView: UICollectionView,sizeForItemATindexPath indexPath: NSIndexPath) -> CGSize {

    let width : CGFloat
    let height : CGFloat

    if indexPath.section == 0 {
        // First section
        width = collectionView.frame.width/7
        height = 50
        return CGSizeMake(width,height)
    } else {
        // Second section
        width = collectionView.frame.width/3
        height = 50
        return CGSizeMake(width,height)
    }

}

将此代码放在collectionViewController类中.将高度设置为细胞高度应该是多少.如果在collectionView中使用单元格或插图之间的间距,则可能需要调整宽度.

大佬总结

以上是大佬教程为你收集整理的ios – 使用UICollectionView具有不同单元大小的部分全部内容,希望文章能够帮你解决ios – 使用UICollectionView具有不同单元大小的部分所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。