[Swift 开发] UICollectionView的用法


//加上UICollectionView的代理
final class V2: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
    
    private var collectionView: UICollectionView?
    private var array: [Int] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let flowlayout = UICollectionViewFlowLayout()
        //滚动方向
        flowlayout.scrollDirection = .Vertical
        flowlayout.itemSize = CGSize(width: UIScreen.mainScreen().bounds.width / 4, height: UIScreen.mainScreen().bounds.width /  4)
        flowlayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        //列间距
        flowlayout.minimumInteritemSpacing = 0.0;
        //行间距
        flowlayout.minimumLineSpacing      = 0.0;
        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: flowlayout)
        
        for i in 0...3300 {
            array.append(i)
        }
        
        // 设置代理
        collectionView?.delegate   = self
        collectionView?.dataSource = self
        
        // 注册
        collectionView?.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        
        if let collectionView = collectionView {
            collectionView.backgroundColor = UIColor.whiteColor()
            view.addSubview(collectionView)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


// MARK: - UICollectionViewDelegate
    //cell的size
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: (UIScreen.mainScreen().bounds.width - 10) / 4, height: (UIScreen.mainScreen().bounds.width - 10) / 4 + 20)
    }
    
    //选中哪行
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print(indexPath.row)
    }
    
    func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
        let temp = array.removeAtIndex(sourceIndexPath.item)
        array.insert(temp, atIndex: destinationIndexPath.item)
    }

// MARK: - UICollectionViewDataSource
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return array.count
    }
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let id = "cell"
        let cell: CollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(id, forIndexPath: indexPath) as! CollectionViewCell
        cell.sizeToFit()
        
        //添加圆角图片
        cell.imageView?.image = UIImage(named: "image_1459912123.971836.jpg")
        cell.imageView?.layer.masksToBounds = true
        cell.imageView?.layer.cornerRadius = 30.0
        
        cell.text?.text = "\(array[indexPath.row])"
        return cell
    }
}

UICollectionView的基本用法,下次用直接过来复制.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值