我想制作一个在水平滚动项目上方带有标题的collectionView。我已经将collectionView的布局设置为可流动,滚动到水平,但是当我检查节标题时,它只会创建一个位于行开头而不是顶部的标题。
我看到有人对此发表评论,但答案很快就过时了5。大约在2013-2015年发布
在下面的图像中,我想在图像视图上方“附近”
导入UIKit
BarCollectionViewController类:NSObject,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
var barList = ["Your Bar", "Bar 2", "Bar 3", "Bar 4", "Bar 5", "Bar 6", "Bar 7", "Bar 8", "Bar 9", "Bar 10"]
var barDistance = 3.1
// MARK: - Table view data source
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return barList.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BarCell", for: indexPath) as! BarCollectionViewCell
cell.barTitle.text = "\(barList[indexPath.row]) \n\(barDistance) Miles"
cell.layer.cornerRadius = 16
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "SectionHeader", for: indexPath) as! SectionHeaderReusableView
return headerView
}
}