自定义cell
import UIKit
class CollectionViewCell: UICollectionViewCell {
var imgView: UIImageView!
var nameLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
self.createCellUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func createCellUI() {
self.imgView = UIImageView.init(frame: CGRect(x: 10, y: 10, width: self.contentView.frame.size.width - 20 , height: self.contentView.frame.size.width - 20))
self.imgView.backgroundColor = .lightGray
self.contentView.addSubview(self.imgView)
self.nameLabel = UILabel.init(frame: CGRect(x: 10, y: self.contentView.frame.size.width - 20 + 10, width: self.contentView.frame.size.width - 20, height: 30))
self.nameLabel.text = "名称"
self.nameLabel.textAlignment = .center
self.contentView.addSubview(self.nameLabel)
}
}
创建collectionView
import UIKit
class CollectionViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
let cell_identifier:String = "collectionCell"
let cell_head:String = "collectionHeadCell"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.addSubview(self.collectionView)
}
//分区数
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 3
}
//每个分区含有的 item 个数
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
//每个cell中的内容
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cell_identifier, for: indexPath)
return cell
}
//返回区头、区尾
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
var headview = CollectionHeaderView();
if kind == UICollectionView.elementKindSectionHeader {
headview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: cell_head, for: indexPath as IndexPath) as! CollectionHeaderView;
headview.label.text = "表头"
}
return headview;
}
//每个分区的内边距
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0);
}
//最小 item 间距
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0;
}
//最小行间距
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0;
}
//item 的尺寸
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width: self.view.frame.size.width / 2, height: self.view.frame.size.width / 2 + 30)
}
//每个分区区头尺寸
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize (width: self.view.frame.size.width, height: 50)
}
//MARK: - 懒加载
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout.init()
let collectionView = UICollectionView.init(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height - 64), collectionViewLayout: layout)
collectionView.backgroundColor = .systemGroupedBackground
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: cell_identifier)
collectionView.register(CollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: cell_head)
return collectionView
}()
}
