UICollectionView系列(一)

UICollectionViewDelegate

UICollectionViewDataSource

UICollectionViewDelegateFlowLayout

UICollectionViewFlowLayout(提供的最基本的流式布局)!

寫一個(gè)簡單的例子來熟悉一下流程

 self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window?.backgroundColor = UIColor.whiteColor()
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSizeMake(100, 100)
        let collectionVC = CollectionViewController(collectionViewLayout: flowLayout)
        self.window?.rootViewController = collectionVC
        self.window?.makeKeyAndVisible()
        // Override point for customization after application launch.
        return true

在這里初始化了CollectionViewController,并設(shè)置為根控制器!

class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {}

自定義的UICollectionViewController,并且實(shí)現(xiàn)了UICollectionViewDelegateFlowLayout代理中的一些方法!
實(shí)現(xiàn)一個(gè)最簡單的帶自定義HeaderFooterUICollectionView需要至少實(shí)現(xiàn)以下幾個(gè)方法:

  • optional func numberOfSectionsInCollectionView(_ collectionView: UICollectionView) -> Int

  • func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int

  • optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize

  • optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize

  • optional func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView

在這里對Cell以及HeaderFooter進(jìn)行注冊

 override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: self.collectionViewLayout)
        self.collectionView?.backgroundColor = UIColor.whiteColor()
        
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        //
        self.collectionView?.registerClass(HeaderCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header")
        self.collectionView?.registerClass(FooterCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "Footer")
        

        // Do any additional setup after loading the view.
    }

返回Section

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 5
    }

返回Section中的Cell數(shù)目

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 10
    }


返回Cell

 override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
        
        cell.backgroundColor = UIColor.redColor()
    
        // Configure the cell
    
        return cell
    }

返回Header和Footer的Size

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSizeMake(100, 40)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSizeMake(100, 40)
    }

返回具體的Header和Footer


 override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        if kind == UICollectionElementKindSectionHeader {
            let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "Header", forIndexPath: indexPath) as! HeaderCollectionReusableView
            headerView.label.text = "Hello !!!"
            headerView.backgroundColor = UIColor.grayColor()
            return headerView
        } else {
            let footerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "Footer", forIndexPath: indexPath) as! FooterCollectionReusableView
            
            footerView.label.text = "World!"
            footerView.backgroundColor = UIColor.greenColor()
            return footerView
        }
        
        
        
        
    }

自定義的Header

class HeaderCollectionReusableView: UICollectionReusableView {
    
    var label: UILabel
    
    
    override init(frame: CGRect) {
        label = UILabel(frame: CGRectMake(0, 0, 100, 40))
        super.init(frame: frame)
        self.addSubview(label)        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容