方法1:创建 的时候注册 layout
/// 时间view private lazy var timeCollectionV: UICollectionView = { 1.直接注册 并设置好 UICollectionViewFlowLayout let layout = UICollectionViewFlowLayout() layout.itemSize = CGSize.init(width: (JY_DEVICE_WIDTH - 60) / 5, height: (JY_DEVICE_WIDTH - 60) / 5) layout.minimumLineSpacing = 10 layout.minimumInteritemSpacing = 0 layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0) let collectionV = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout) collectionV.register(UINib.init(nibName: "JYChooseShopTimeCollectionCell", bundle: nil), forCellWithReuseIdentifier: "JYChooseShopTimeCollectionCell") collectionV.delegate = self collectionV.dataSource = self collectionV.backgroundColor = UIColor.white return collectionV }()
2.在代理里面设置UICollectionViewFlowLayout
extension JYBespeakStylistAndTimeVc: UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
//最小行间距 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 10; } //每个分区的内边距 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsetsMake(0, 0, 0, 0); } //item 的尺寸 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let sizeItem : CGFloat = CGFloat(JY_DEVICE_WIDTH - 60) / 5 return CGSize.init(width: sizeItem, height: sizeItem) }
}
3.其他设置
// 一个分区几行 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 60 } //每个cell func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "JYChooseShopTimeCollectionCell", for: indexPath) as! JYChooseShopTimeCollectionCell // cell.configModel(model: self.secmentControlTuple.timeArr![indexPath.row]) return cell }
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
DDLOG(message: "点击 item")
}