swift - 标题中的不同标签取决于集合视图中的部分
问题描述
let titles = ["Popularity","Top Rated","Release Date","Revenue"]
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: Header.identifier, for: indexPath) as! Header
header.label.text = titles[indexPath.section]
return header
}
我想做的标题文本会根据每个部分中的标题数组而有所不同。我可以看到标题中的空白,但其中没有标题。
这是我的 Header 类:
class Header: UICollectionReusableView {
static let identifier = "Header"
let label = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
label.frame = bounds
}
}
同样在 viewDidLoad() 中,我写了这个。
collectionV.register(Header.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: Header.identifier)
我错过了什么?
解决方案
推荐阅读
- python - 如何更改解释器输出形状?
- azure - 将多个 Windows 计划任务从 VM 移植到单个 Web 作业
- javascript - 在 json 数据中写入时,onclick 属性未绑定在 html 代码中
- excel - 获取由宏打开/创建的 Excel 工作簿的灰屏
- python - if 语句发出 ply py
- html - 外部 CSS 不起作用,但内部 CSS
- python - 通过 Python 中的类方法访问局部变量
- reactjs - React Native Navigation,如何在 Class Components 之间传递和使用 Props?
- google-cloud-bigtable - 从google bigtable中删除空行的有效方法
- ios - 谷歌地图在多个纬度经度之间绘制折线