• UICollectionView swift2模版


    class testViewController:BaseViewController,UICollectionViewDataSource, UICollectionViewDelegate , UICollectionViewDelegateFlowLayout{
        lazy var myCollectionView:UICollectionView = {
            let layout = UICollectionViewFlowLayout()
            layout.minimumLineSpacing = 1.5  //上下间隔
            layout.minimumInteritemSpacing = 1 //左右间隔
            let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout:layout)
            collectionView.backgroundColor = UIColor.whiteColor()
            return collectionView
        }()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            //加载页面元素
            self.addSubView()
            self.makeConstraints()
            self.myCollectionView.reloadData()
        }
        
        func addSubView(){
            self.view.backgroundColor = UIColor(rgba: "#F1F1F1")
            self.myCollectionView.delegate = self
            self.myCollectionView.dataSource = self
            
            //注册header
            self.myCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "TESTHeader")
            
            self.myCollectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "TESTCell")
            self.view.addSubview(myCollectionView)
        }
        
        //增加约束
        func makeConstraints(){
            //Add Constraints
            myCollectionView.snp_makeConstraints{make in
                make.top.bottom.left.right.equalTo(self.view)
            }
        }
        
        //MARK: - CollectionView 代理方法
        func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
            //分栏数量
            return 10
        }
        
        func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            //每个分栏中Cell的个数
            return 2
        }
        
        func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
            var reuseView:UICollectionReusableView
            reuseView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "TESTHeader", forIndexPath: indexPath)
            reuseView.backgroundColor = UIColor.blackColor()
            return reuseView
        }
        
        ///cell内容
        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            //返回复用的cell
            var cell = collectionView.dequeueReusableCellWithReuseIdentifier("TESTCell", forIndexPath: indexPath)
            if indexPath.row % 2 == 0 {
                cell.backgroundColor = UIColor.redColor()
            }else{
                cell.backgroundColor = UIColor.blueColor()
            }
            return cell
        }
        
        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
            //返回每个cell的大小
            return CGSize( KMainScreenWidth, height:150)
        }
        
        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
            var h:CGFloat = 15
            return CGSizeMake(KMainScreenWidth,h)
        }
        
        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
            //返回sectionview的大小
            var h:CGFloat = 50
            return CGSizeMake(KMainScreenWidth,h)
        }
        
        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
            return UIEdgeInsets(top:0, left: 0, bottom:5, right: 0)
        }
        
        func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
            //Cell点击事件
        }
    }
    
  • 相关阅读:
    吴裕雄--天生自然C++语言学习笔记:C++ 引用
    吴裕雄--天生自然C++语言学习笔记:C++ 指针
    吴裕雄--天生自然C++语言学习笔记:C++ 字符串
    吴裕雄--天生自然C++语言学习笔记:C++ 数组
    吴裕雄--天生自然C++语言学习笔记:C++ 数字
    吴裕雄--天生自然C++语言学习笔记:C++ 函数
    吴裕雄--天生自然C++语言学习笔记:C++ 判断
    HiHoCoder1671 : 反转子串([Offer收割]编程练习赛41)(占位)
    HihoCoder1670 : 比赛日程安排([Offer收割]编程练习赛41)(模拟)
    POJ3417Network(LCA+树上查分||树剖+线段树)
  • 原文地址:https://www.cnblogs.com/wyxy2005/p/5196967.html
Copyright © 2020-2023  润新知