UICollectionView系列(一)

UICollectionViewDelegate

UICollectionViewDataSource

UICollectionViewDelegateFlowLayout

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

写一个简单的例子来熟悉一下流程

 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,并设置为根控制器!

class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {}

自定义的UICollectionViewController,并且实现了UICollectionViewDelegateFlowLayout代理中的一些方法!
实现一个最简单的带自定义HeaderFooterUICollectionView需要至少实现以下几个方法:

  • 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进行注册

 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数目

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")
    }
}

你可能感兴趣的:(UICollectionView系列(一))