如果我没有指定滚动方向,则会正确添加标题。但是,如果我指定它,单元格将被标题视图替换。只显示标题视图。怎么解决这个?请帮忙!
class HongbeiCollectionView: UICollectionView,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate {
var hongbeiData : [Hongbei]!
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
// layout.headerReferenceSize = CGSize(width: frame.width, height: 10)
super.init(frame: frame, collectionViewLayout: layout)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
print("Hongbei")
return hongbeiData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
print("cell is being called")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HongbeiCell", for: indexPath) as! HongbeiCell
cell.hongbeiButton.sd_setImage(with: URL.init(string: hongbeiData[indexPath.item].video_img ?? ""), for: .normal, placeholderImage: #imageLiteral(resourceName: "moren-2"))
cell.hongbeiButton.clipsToBounds = true
cell.hongbeiButton.layer.cornerRadius = 10.0
print("title is \(hongbeiData[indexPath.item].title!)")
cell.hongbeiLabel.text = hongbeiData[indexPath.item].title!
cell.setID(id: hongbeiData[indexPath.item].video_id!)
cell.hongbeiButton.setTitle("Button \(indexPath)", for: .normal)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
return CGSize(width: 150, height: 150)
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView{
print("viewForSupplementaryElementOfKind is called")
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderForCollectionView", for: indexPath as IndexPath) as! HeaderForCollectionView
headerView.labelHeader.text = "Hongbei"
return headerView
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
print("referenceSizeForHeaderInSection")
return CGSize(width: collectionView.frame.width, height: 30)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{
return UIEdgeInsets(top: 0, left: 20.0, bottom: 0, right: 0.0)
}
}
如果我指定referenceSizeForHeaderInSection's
宽度小于collectionview.frame.width
,则首先显示标题视图,然后显示单元格。
此外,如果您注意到OneyuanZone CollectionView是垂直滚动并且其标题已正确添加。
答案 0 :(得分:1)
在下面的代码中,
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize
{
print("referenceSizeForHeaderInSection")
return CGSize(width: collectionView.frame.width, height: 30)
}
您提供的标题的宽度是完整的collectionView's
宽度。这是细胞不可见的原因。尝试减少它,以便您也可以看到细胞。
请尝试使用以下代码。
CGSize(width: collectionView.frame.width - 50, height: 30)
修改强>
查看层次结构屏幕截图:
如果您仍然遇到任何问题,请告诉我。