一、cell复用的collectionView:
1.HomeRecommendView.swift 文件 & HomeRecommendView.xib 文件:
import UIKit
class HomeRecommendView: UIView {
@IBOutlet weak var collectionView: UICollectionView!
var selectBlock:((Int)->())?
lazy var layout: UICollectionViewFlowLayout = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.sectionInset = UIEdgeInsets.init(top: 10, left: 16, bottom: 10, right: 16)
layout.scrollDirection = .horizontal
layout.itemSize = CGSize.init(width: kScreenWidth / 3.0, height: 119.5)
return layout
}()
var dataSource: [HomeBookReadModel]? {
didSet {
guard dataSource != nil else {
return
}
collectionView.reloadData()
}
}
override func awakeFromNib() {
collectionView.delegate = self
collectionView.dataSource = self
let nib = UINib(nibName: "HomeRecommendCollectionCell", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: "HomeRecommendCollectionCell")
collectionView.setCollectionViewLayout(layout, animated: true)
}
}
// MARK: - UICollectionViewDelegate,UICollectionViewDataSource
extension HomeRecommendView: UICollectionViewDelegate,UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataSource?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeRecommendCollectionCell", for: indexPath) as! HomeRecommendCollectionCell
let model = dataSource?[indexPath.row]
cell.model = model
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectBlock?(indexPath.row)
}
}
- 代码实现collectionView:
lazy var collectionView: UICollectionView = {
let frame = CGRect.init(x: 0, y: 0, width: self.view.bounds.size.width, height:kScreenHeight)
let collectionView = UICollectionView(frame: frame, collectionViewLayout: self.layout)
collectionView.backgroundColor = UIColor.white
collectionView.delegate = self
collectionView.dataSource = self
return collectionView
}()
2.自定义 HomeRecommendCollectionCell.swift 文件 & HomeRecommendCollectionCell.xib 文件
二、sizeForItem 动态改变:
如果想让 item 的宽高自适应,则不需要在创建 UICollectionViewFlowLayout 实例的时候设置 layout.itemSize ,只需要遵循 UICollectionViewDelegateFlowLayout 代理,重写 sizeForItemAt 代理方法即可。
extension ReleaseEvaluationThirdCell: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let model = self.dataSource?[indexPath.row]
return model?.itemSize ?? CGSize.init(width: 0, height: 0)
}
//如果写了 layout.minimumLineSpacing = 20 可以不写这个代理方法
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 20
}
//如果写了 layout.minimumInteritemSpacing = 20 可以不写这个代理方法
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 20
}
}
- 注:这种方法的效果并不好,并不能让所有item之间的左右间距固定,想要左右间距固定,需要重写 UICollectionViewFlowLayout 类。
三、增加header、footer:
- 注释:在collectionView中,并不像tableView一样,有tableViewHeader、tableViewFooter,只能添加到section的header、footer上,并且必须实现 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) 代理方法,header才能正常展示,当不想显示header的时候,需要 return CGSize.zero 。
//注册header
collectionView.register(BookMuseumChineseHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "BookMuseumChineseHeaderView")
//MARK: - UICollectionViewDelegate,UICollectionViewDataSource
extension BookMuseumChineseController: UICollectionViewDelegate,UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "BookMuseumChineseHeaderView", for: indexPath) as! BookMuseumChineseHeaderView
headerView.dataSource = self.headerModel
return headerView
}
return UICollectionReusableView()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if self.dataModel?.booklist != nil {
return CGSize(width: collectionView.frame.size.width, height: self.dataModel?.headerHeight ?? 0)
} else {
return CGSize.zero
}
}
}