swift3.0 collectionView 基本用法

前端之家收集整理的这篇文章主要介绍了swift3.0 collectionView 基本用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

留着拷贝用 为啥代码提示这么不爽-_-!!

//
//  PageTitleView.swift
//  MiMark
//
//  Created by 1 on 16/9/14.
//  Copyright © 2016年 dacai. All rights reserved.
//

import UIKit

// MARK:- 定义协议
protocol PageTitleViewDelegate : class {
    func pageTitleView(_ titleView : PageTitleView,selectedIndex index : Int)
}

// MARK:- 定义常量
private let kScrollLineH : CGFloat = 2
private let kNormalColor : (CGFloat,CGFloat,CGFloat) = (85,85,85)
private let kSelectColor : (CGFloat,CGFloat) = (255,128,0)
private let kPageTitleCellID = "kPageTitleCellID"
private let kPageTitleHeadID = "kPageTitleHeadID"
private let kPageTitleFootID = "kPageTitleFootID"
private let kPageTitleCellW : CGFloat = 120
private let kPageTitleCellH : CGFloat = 40

// MARK:- 定义PageTitleView类
class PageTitleView: UIView {
    
    // MARK:- 定义属性
    fileprivate var currentIndex : Int = 0
    fileprivate var titles : [String]
    weak var delegate : PageTitleViewDelegate?
    
    // MARK:- 懒加载属性
    lazy var collectionView : UICollectionView = {[unowned self] in
        // 1.创建布局
        let layout = UICollectionViewFlowLayout()
        //layout.itemSize = CGSize(width: kPageTitleCellW,height: kPageTitleCellH)
        //layout.minimumLineSpacing = 10
        //layout.minimumInteritemSpacing = 0
        //layout.headerReferenceSize = CGSize(width: kScreenW,height: 20)
        //layout.footerReferenceSize = CGSize(width: kScreenW,height: 20)
        layout.sectionInset = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 0)
        
        // 2.创建UICollectionView
        let collectionView = UICollectionView(frame: CGRect(x: 0,y: 0,width: kScreenW,height: 400),collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.orange
        
        collectionView.dataSource  = self
        collectionView.delegate = self
        collectionView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
        
        //collectionView.register(UINib(nibName: "CollectionNormalCell",bundle: nil),forCellWithReuseIdentifier: kPageTitleCellID)
        
        //collectionView.register(UINib(nibName: "CollectionHeaderView",forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,withReuseIdentifier: kHeaderViewID)
        
        collectionView.register(PageTitleCollectionViewCell.self,forCellWithReuseIdentifier: kPageTitleCellID)
        
        collectionView.register(PageReusableView.self,withReuseIdentifier: kPageTitleHeadID)
        
        collectionView.register(PageFootReusableView.self,forSupplementaryViewOfKind: UICollectionElementKindSectionFooter,withReuseIdentifier: kPageTitleFootID)
        
        
        
        
        return collectionView
        }()
    
    
    
    // MARK:- 自定义构造函数
    init(frame: CGRect,titles : [String]) {
        self.titles = titles
        
        super.init(frame: frame)
        
        //设置UI界面
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK:- 设置UI
extension PageTitleView {
    func setupUI(){
        self.addSubview(collectionView)
    }
}


// MARK:- 代理方法
extension PageTitleView : UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1;
    }
    
    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        return 100;
    }
    
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kPageTitleCellID,for: indexPath) as! PageTitleCollectionViewCell
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView,viewForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) -> UICollectionReusableView {
        
        if kind == UICollectionElementKindSectionHeader {
            // 1.取出HeaderView
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,withReuseIdentifier: kPageTitleHeadID,for: indexPath) as! PageReusableView
            return headerView
        }else{
            // 1.取出HeaderView
            let footView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,withReuseIdentifier: kPageTitleFootID,for: indexPath) as! PageFootReusableView
            return footView
        }
    }
    
    func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: kScreenW,height: 200)
    }
    
    func collectionView(_ collectionView: UICollectionView,referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: kScreenW,height: 100)
    }
    
    func collectionView(_ collectionView: UICollectionView,insetForSectionAt section: Int) -> UIEdgeInsets {
        
        return UIEdgeInsetsMake(10,0) //上、左、下、右距四边的间距
    }
    
    func collectionView(_ collectionView: UICollectionView,sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        return CGSize(width: 10,height: 10)
    }
    
    func collectionView(_ collectionView: UICollectionView,minimumLineSpacingForSectionAt section: Int) -> CGFloat{
        return 5
    }
    
    func collectionView(_ collectionView: UICollectionView,minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{
        return 5;
    }
    
    
    
    
    
    
    
}
原文链接:/swift/322857.html

猜你在找的Swift相关文章