【发布时间】:2016-04-28 00:57:54
【问题描述】:
我创建了一个简单的 collectionView 并将单元格设置为显示每个单元格在单击时进入的屏幕预览。这一切都很好,所以我将继续为每个部分添加标题。
但是,问题来了……
当我将这些部分添加到代码中时,它会导致 collectionView 消失(或者至少除了白屏之外它不显示任何其他内容)
这是我添加(或修改)的代码,如果发布的代码不够,请告诉我,我会发布更多...
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let sectionHeaderView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "SectionHeader", forIndexPath: indexPath) as! SectionHeaderView
if let title = papersDataSource.titleForSectionAtIndexPath(indexPath) {
sectionHeaderView.title = title
}
return sectionHeaderView
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return papersDataSource.numberOfSections
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return papersDataSource.numberOfPapersInSection(section)
}
我设置了一个 UICollectionReusableView 的子类,它具有以下代码...
import UIKit
class SectionHeaderView: UICollectionReusableView {
@IBOutlet weak var titleLabel: UILabel!
var title: String? {
didSet {
titleLabel.text = title
}
}
}
一切正常,但显示完全空白。
有什么想法吗?
【问题讨论】:
-
阅读苹果文档,看来你必须重写这个
UICollectionViewLayout方法:layoutAttributesForSupplementaryViewOfKind:atIndexPath:。我认为该方法应该返回:.SupplementaryView
标签: swift header uicollectionview uicollectionreusableview