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点击事件
        }
    }
    
  • 相关阅读:
    mapr
    短信 流控规则
    js modify local file
    An O(ND) Difference Algorithm and Its Variations (1986)
    美团金融扫码付静态资源加载优化实践
    前端遇上Go: 静态资源增量更新的新实践
    小程序短信验证码登录的实现与优化
    A Practical Introduction to Blockchain with Python
    numpy计算
    小程序登录方式切换 不做url跳转
  • 原文地址:https://www.cnblogs.com/wyxy2005/p/5196967.html
Copyright © 2011-2022 走看看