zoukankan      html  css  js  c++  java
  • 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点击事件
        }
    }
    
  • 相关阅读:
    半条命2引擎:Source 关于Multiplayer Networking
    关于毕设3DGPSR程序bug修改
    My 2D SketchBased 3D Shape Retrieval
    Manifold Harmonics Transform BY ARPACK++
    c语言错误代码
    C语言system函数用法
    win7下开启梦幻桌面
    Win7与xp双系统的安装
    C 语言中的控制结构
    Win7下安装xp虚拟机
  • 原文地址:https://www.cnblogs.com/wyxy2005/p/5196967.html
Copyright © 2011-2022 走看看