IOS学习笔记之UICollectionView的使用

let layout = UICollectionViewFlowLayout()
        //设置滑动的方向
        layout.scrollDirection = .vertical
        //设置cell的宽高
        layout.itemSize = CGSize.init(width: (width - 48) / 2, height: 96)
        //设置行于行之间的间隙
        layout.minimumLineSpacing = 16
        //设置列于列之间的间隙
        layout.minimumInteritemSpacing = 16
        //设置外边距,这个外边距不会改变header和footer的边距(这个很重要,画重点)
        layout.sectionInset = UIEdgeInsets.init(top: 0, left: 16, bottom: 16, right: 16)
        //设置尾部视图size
        layout.footerReferenceSize = CGSize(width: width, height: 66)
        
        mCollectionView.delegate = self
        mCollectionView.dataSource = self
        mCollectionView.collectionViewLayout = layout
        
        //注册尾部视图
        mCollectionView.register(TopUpFooter.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: footerIdentifier)

设置头部视图和尾部视图的用法类似,在注册完之后在viewForSupplementaryElementOfKind方法中进行视图的设置。

 //添加header
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionFooter{
            let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: footerIdentifier, for: indexPath) as! TopUpFooter
            return footer
        }
        return UICollectionReusableView()
    }

其他的数据协议和回调协议跟TableView几乎一样,所以就不再赘述了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值