swift (3.3)UICollectionView 的 SectionHeader问题

在使用Swift 3.3开发时,遇到UICollectionView SectionHeader无法显示的问题。原因是代理方法在这一版本中有所改变,原有的实现方式不再适用。通过调整代理方法并根据个人需求定制SectionHeader,最终解决了这个问题,强调代码需注重细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(SectionHeaderView 不显示问题)
自己在写UICollectionView的时候 想设置SectionHeaderView的时候。怎么都不显示,layout 的布局也设置了,包括注册 sectionHeaderView

  collectionView.register(YMCollectionHeaderView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: reusedId)
 // section headerView 的高
        layout.headerReferenceSize = CGSize(width: SCREENW, height: 100);

最后发现一个版本的问题 , 因为比较懒,代理方法直接是copy的,但是swift3.3 的代理方法已经稍微变化,导致网上的方法不走。
网上方法:

   func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        //判断如果 kind 是footerView类型
        if kind == UICollectionElementKindSectionFooter {
            //创建footerView, 填写footerView类型(kind)、复用标识符、indexPath
            footerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: reusedId, forIndexPath: indexPath)
            //设置footerView背景颜色
            footerView.backgroundColor = UIColor.redColor()

            return footerView
        }
        return footerView
    }

但是swift3.3 方法已经稍微不同,外部参数已经处理掉了

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        var reusableview:YMCollectionHeaderView!

        if kind == UICollectionElementKindSectionHeader
        {
            reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: reusedId, for: indexPath) as! YMCollectionHeaderView
        }
        else if kind == UICollectionElementKindSectionFooter
        {

        }

        return reusableview
    }

在根据自己的一些要求来处理sectionHeaderView。

———–代码还是用心的好。
-谨记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值