问题描述
我有一个collectionView,并且每个部分都需要一个标题。标头非常简单,左侧只有UILabel
作为标题。
我已经创建了HeaderView:
class HeaderCollectionView: UICollectionReusableView {
static let kIdentifier = "kHeaderCollectionView"
private lazy var titleLabel: UILabel = {
let _titleLabel = UILabel()
_titleLabel.textColor = .black
_titleLabel.font = UIFont(descriptor: UIFontDescriptor.preferredFontDescriptor(withTextStyle: .title1),size: 15.0)
_titleLabel.numberOfLines = 0
_titleLabel.textAlignment = .left
_titleLabel.allowsDefaultTighteningForTruncation = true
return _titleLabel
}()
var title: String? {
didSet {
titleLabel.text = title
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
func setupUI() {
addSubview(titleLabel)
titleLabel.snp.makeConstraints { (make) in
make.top.left.right.bottom.equalToSuperview()
make.height.equalTo(50)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我正在正确设置它:
extension HomeViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return viewmodel.categories.count
}
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
let category = viewmodel.categories[section]
return category.routines.count
}
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,referenceSizeforHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width,height: 180.0)
}
func collectionView(_ collectionView: UICollectionView,viewForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionView.elementKindSectionHeader:
let category = viewmodel.categories[indexPath.section]
let cell = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader,withReuseIdentifier: HeaderCollectionView.kIdentifier,for: indexPath) as! HeaderCollectionView
cell.title = category.title
return cell
default:
fatalError("Unexpected Kind")
}
}
private func setupCollectionView() {
homeView.collectionView.dataSource = self
homeView.collectionView.delegate = self
homeView.collectionView.backgroundColor = .clear
homeView.collectionView.register(ImageTextCollectionViewCell.self,forCellWithReuseIdentifier: ImageTextCollectionViewCell.kIdentifier)
homeView.collectionView.register(HeaderCollectionView.self,forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,withReuseIdentifier: HeaderCollectionView.kIdentifier)
homeView.collectionView.layer.speed = 2.5
homeView.collectionView.showsHorizontalScrollIndicator = false
}
}
尽管如此,即使以为是委托的代表,视图上的高度始终是错误的,但宽度是正确的。
有什么想法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)