[Swift 开发] UICollectionView的用法

前端之家收集整理的这篇文章主要介绍了[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的基本用法,下次用直接过来复制. 原文链接:https://www.f2er.com/swift/324063.html

猜你在找的Swift相关文章