let layout = UICollectionViewFlowLayout()
//设置滑动的方向
layout.scrollDirection = .vertical
//设置cell的宽高
layout.itemSize = CGSize.init(width: (width - 48) / 2, height: 96)
//设置行于行之间的间隙
layout.minimumLineSpacing = 16
//设置列于列之间的间隙
layout.minimumInteritemSpacing = 16
//设置外边距,这个外边距不会改变header和footer的边距(这个很重要,画重点)
layout.sectionInset = UIEdgeInsets.init(top: 0, left: 16, bottom: 16, right: 16)
//设置尾部视图size
layout.footerReferenceSize = CGSize(width: width, height: 66)
mCollectionView.delegate = self
mCollectionView.dataSource = self
mCollectionView.collectionViewLayout = layout
//注册尾部视图
mCollectionView.register(TopUpFooter.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: footerIdentifier)
设置头部视图和尾部视图的用法类似,在注册完之后在viewForSupplementaryElementOfKind方法中进行视图的设置。
//添加header
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionView.elementKindSectionFooter{
let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: footerIdentifier, for: indexPath) as! TopUpFooter
return footer
}
return UICollectionReusableView()
}
其他的数据协议和回调协议跟TableView几乎一样,所以就不再赘述了。