我正在嘗試為 UICollectionViewCompositionalLayout 中的每個部分創建描述性標題。像這樣的東西:

這是我的標題:
class CafeHeaderView: UICollectionReusableView {
static let reuseId = "HeaderView"
let categoryLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
setupContent()
}
func setupContent() {
categoryLabel.frame = CGRect(x: 16, y: 15, width: 200, height: 17)
categoryLabel.font = UIFont(name: "Montserratarm-Medium", size: 22)
categoryLabel.text = "Section ?? "
addSubview(categoryLabel)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我注冊了我的手機
mainCollectionView.register(CafeHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: CafeHeaderView.reuseId)
比創建的邊界補充項
section.boundarySupplementaryItems = [.init(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .estimated(60)), elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)]
最后
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: CafeHeaderView.reuseId, for: indexPath)
}
那么有沒有辦法確定當前部分并相應地設定我的標簽文本?
uj5u.com熱心網友回復:
試試這個:
var anyArray = ["cat", "dog", "mouse", "elephant"]
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: CafeHeaderView.reuseId, for: indexPath)
header.yourLabel.text = anyArray[indexPath.section]
return header
}
要在每個單元格的部分中存盤資料,請使用indexPath.section。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/399870.html
標籤:迅速 合适的视图 用户界面 uicollectionviewlayout uicollectionreusableview
上一篇:具有通用資料型別的Swift下標
