美文网首页工作生活
iOS Swift UICollectionView 基础用法

iOS Swift UICollectionView 基础用法

作者: Yuency | 来源:发表于2019-07-04 16:15 被阅读0次

前言:

写了100年的UICollectionView,没想到还是有提笔忘字的一天。下次不会了,因为可以直接抄。

Swift
代码地址:https://github.com/gityuency/Autolayout
示例代码类名 【ExampleOfUICollectionViewController】

效果图 (从模拟器截图,把宽度改成 500px,就是下面的效果,图片不会显得过大)

截图.png

控制器代码如下:


import UIKit

class ExampleOfUICollectionViewController: UIViewController {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.delegate = self
        collectionView.dataSource = self
        
        //XIB加载
        collectionView?.register(UINib(nibName: ExampleHeaderView.reuseId, bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: ExampleHeaderView.reuseId)
        //代码加载
        collectionView?.register(ExampleFooterView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: ExampleFooterView.reuseId)
        //XIB加载
        collectionView?.register(UINib.init(nibName: ExampleCell.reuseId, bundle: nil), forCellWithReuseIdentifier: ExampleCell.reuseId)
        //代码加载
        collectionView.register(ExampleCodeCell.self, forCellWithReuseIdentifier: ExampleCodeCell.reuseId)
    }
}

extension ExampleOfUICollectionViewController: UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return Int(arc4random_uniform(10)) + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return Int(arc4random_uniform(10)) + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if indexPath.section == 0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ExampleCodeCell.reuseId, for: indexPath) as! ExampleCodeCell
            return cell
        } else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ExampleCell.reuseId, for: indexPath) as! ExampleCell
            return cell
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        if kind == UICollectionView.elementKindSectionHeader {
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind:  UICollectionView.elementKindSectionHeader, withReuseIdentifier: ExampleHeaderView.reuseId, for: indexPath) as! ExampleHeaderView
            return headerView
        } else if kind == UICollectionView.elementKindSectionFooter {
            let footerView = collectionView.dequeueReusableSupplementaryView(ofKind:  UICollectionView.elementKindSectionFooter, withReuseIdentifier: ExampleFooterView.reuseId, for: indexPath) as! ExampleFooterView
            return footerView
        }
        return UICollectionReusableView()
    }
}


extension ExampleOfUICollectionViewController: UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 0 {
            return CGSize(width: 40, height: 40)
        } else {
            return CGSize(width: 70, height: 70)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: 1, height: 20)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: 1, height: 20)
    }
}

extension ExampleOfUICollectionViewController: UICollectionViewDelegate {
    
}


其中还有一些控件使用代码或者带上XIB创建出来的


import UIKit

class ExampleCell: UICollectionViewCell {

    static let reuseId = "ExampleCell"
    
    override func awakeFromNib() {
        super.awakeFromNib()
    
    }
}

报错: Could not cast value of type 'UICollectionReusableView' (0x1135d3be0) to 'AutoLayout.ExampleHeaderView' (0x107c27a68).

  1. 打开 XIB
  2. 选择右边第三个 "show theh identity inspector"
  3. 把类名改成我们当前的这个类名, 重新运行

import UIKit

class ExampleHeaderView: UICollectionReusableView {
    
    static let reuseId = "ExampleHeaderView"

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
}

相关文章

网友评论

    本文标题:iOS Swift UICollectionView 基础用法

    本文链接:https://www.haomeiwen.com/subject/xperhctx.html