加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[Swift 开发] UICollectionView的用法

发布时间:2020-12-14 07:05:04 所属栏目:百科 来源:网络整理
导读://加上UICollectionView的代理final class V2: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource { private var collectionView: UICollectionView? private var array: [Int] = [] override func viewDidLoad() { super.viewDidLoad(


//加上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的基本用法,下次用直接过来复制.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读