swift 中UICollection的一些简单的用法

//1 viewDidLoad 里面

overridefunc viewDidLoad() {

       super.viewDidLoad()

        self.view.backgroundColor =UIColor.whiteColor()

        let flowLayout =UICollectionViewFlowLayout()//瀑布流

        //布局collection

        

       var collection = UICollectionView(frame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height), collectionViewLayout: flowLayout)

        collection.backgroundColor =UIColor.yellowColor()

        //flowLayout.minimumLineSpacing = 10.0;//行间距(最小值)

        //flowLayout.minimumInteritemSpacing = 50.0;//item间距(最小值)

        collection.delegate =self

        collection.dataSource =self

      collection.registerClass(SCollectionViewCell.self, forCellWithReuseIdentifier:"cell")

       //这个是关键要注册cell

       self.view.addSubview(collection)

        // Do any additional setup after loading the view.

    }


//2 实现代理方法

func collectionView(collectionView:UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath) -> UICollectionViewCell {

       var cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)

            as!UICollectionViewCell

        cell.backgroundColor =UIColor.redColor()

       return cell

    }

   func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

       return 20 //返回cell的个数

    }

   func numberOfSectionsInCollectionView(collectionView:UICollectionView) -> Int{

    return 1 //区间

    }

    

    

    

    //layout的布局

   func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

       if (indexPath.row %2 != 0 && indexPath.row %3 != 0) {

           return CGSizeMake(140,70);//长的

        }else{

           return CGSizeMake(70,70);//方的

        }

    }

//两个cell的间隔

   func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {

       return UIEdgeInsetsMake(5,5, 5, 5)

    }

//选中cell 

   func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

       print(indexPath.row)

    }

    







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值