Osheep

时光不回头,当下最重要。

swift4.0 UICollectionView

该collectionView 是写在一个自定义view里,作为一个下拉的分类列表,实现分类选择

你孤独,是因为你站的不够高

代码实现
import Foundation
import UIKit

class ProfessionTypeView: BaseView,UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    var mCollectionView:UICollectionView?
    var mDataArr:NSArray? //存储数据
    
    //block 反向传值
    typealias SelectBlock = (Int)->()
    var selectBlock:SelectBlock?
    
    var mSelect_index : Int = 0 {
        //该值已经变化后,刷新collectionView
        didSet {
            mCollectionView?.reloadData()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.white
        
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize(width:(kScreenW-40-35)/3,height:34)
        layout.minimumLineSpacing = 20
        layout.minimumInteritemSpacing = 17.5
        layout.sectionInset = UIEdgeInsetsMake(20, 15, 20, 15)
        
        mCollectionView = UICollectionView.init(frame: CGRect(x:0,y:106,width:frame.size.width,height:frame.size.height-86-49), collectionViewLayout: layout)
        mCollectionView?.backgroundColor = UIColor.white
        mCollectionView?.delegate = self
        mCollectionView?.dataSource = self
        mCollectionView?.showsVerticalScrollIndicator = false
        mCollectionView?.register(ProfessionTypeViewCCell.self, forCellWithReuseIdentifier: "ProfessionTypeViewCCell")
        self.addSubview(mCollectionView!)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1;
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return mDataArr!.count;
    }
   
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let row = indexPath.row
        
        let cateModel = mDataArr?[row] as! ProfessionCateModel
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProfessionTypeViewCCell", for: indexPath) as! ProfessionTypeViewCCell
        cell.mTextLabel.text = cateModel.title
        if mSelect_index == row {
           cell.mTextLabel.backgroundColor = red_color()
           cell.mTextLabel.textColor = white_color()
        }else{
           cell.mTextLabel.backgroundColor = gray_color()
           cell.mTextLabel.textColor = black_color()
        }
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectBlock?(indexPath.row)
    }
    
}


import Foundation
class ProfessionTypeViewCCell: UICollectionViewCell {
   
 var mTextLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        mTextLabel = UILabel.init(frame: CGRect(x:0,y:0,width:frame.size.width,height:frame.size.height))
        mTextLabel.font = UIFont.systemFont(ofSize: 14)
        mTextLabel.textAlignment = NSTextAlignment.center
        mTextLabel.layer.masksToBounds = true
        mTextLabel.layer.cornerRadius = 18;
        self.contentView.addSubview(mTextLabel)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
《swift4.0 UICollectionView》

效果图.png
看似简单,不过初学者的我还是遇到些麻烦

1、遵循代理后会报错,只有把相关代理方法都写完,编译运行才可以,并不是当前写的代码有什么问题
2、mSelect_index需要从外界传值,然后改变选中的位置,刚开始就想着oc中的set方法,最开始是通过一个中间值和set{}方法,才改变,后来经过查找可以直接在该值中实现didSet {}方法,在该值改变后去做操作

点赞