我有一个带有多个标题的UICollectionView
。
它们被实现为节标题。
每个头都属于自己的控制器,这些控制器符合MyHeaderProtocol
。
基本上我有一个控制器数组,并在必要时从每个控制器请求标题单元格。
所以:
collectionView(:viewForSupplementaryElementOfKind:at)
被调用并返回实际视图。视图设置正确,框架尺寸很好等等。以防我将颜色视图变为紫色 - 但我无法在屏幕上看到它。collectionView(_:layout:referenceSizeForHeaderInSection:)
并返回良好的CGSize值。 显示了集合视图,但我看到的只是空白空格而不是所有这些标题。 所以问题是 - 两个必要的方法都显示一个标题部分被调用但是没有显示节标题(但是为它保留了空格)。
以下是我的代码的简化版本:
protocol MyHeaderProtocol: class {
var nib: UINib { get }
var cellReuseIdentifier: String { get }
// Will dequeue UICollectionViewCell using dequeueReusableSupplementaryView and set data for this cell
func setupHeader(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: IndexPath) -> UICollectionViewCell?
func getCellHeight()
}
class MyCollectionView: UIViewController {
@IBOutlet private weak var collectionView: UICollectionView
private var headers: [MyHeaderProtocol] = []
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
// it will instantiate controllers conformed to MyHeaderProtocol and append them to header array
setUpHeaders()
registerHeaders()
}
private func setUpHeaders() {
let headerOne = SimpleHeader1(withModel: viewModel)
headers.append(headerOne)
let headerTwo = SimpleHeader2(withModel: viewModel)
headers.append(headerTwo)
}
private func registerHeaders() {
for header in headers {
collectionView.register(header.nib, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: header.cellReuseIdentifier)
}
}
}
extension MyCollectionView: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return headers.count + 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == headers.count {
// If section isn't a header section - we return actual cells
return viewModel.people.count
} else {
// if it is a header - we return only section, no intems
return 0
}
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
var view : UICollectionReusableView?
switch kind {
case UICollectionElementKindSectionHeader:
// if it is a header
if indexPath.section < headers.count {
let header = headers[indexPath.section]
view = header.setupHeader(collectionView: collectionView, cellForItemAtIndexPath: indexPath)
// to test that header is visible
view?.backgroundColor = UIColor.purple
}
case UICollectionElementKindSectionFooter:
break
default:
assert(false, "Unexpected element kind")
break
}
return view ?? UICollectionReusableView()
}
}
extension MyCollectionView : UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
var size = CGSize.zero
// If that's header - return non-Zero size
if section < headers.count {
let header = headers[section]
size = CGSize.init(width: collectionView.bounds.width, height: header.getCellHeight())
}
return size
}
}
答案 0 :(得分:0)
我认为问题可能出在您的自定义MyHeaderProtocol
协议上。特别是这个功能:
func setupHeader(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: IndexPath) -> UICollectionViewCell?
你必须在这里返回一个标题,否则这个函数collectionView(:viewForSupplementaryElementOfKind:at
将不会产生预期的行为:
var view : UICollectionReusableView?
view = header.setupHeader(collectionView: collectionView, cellForItemAtIndexPath: indexPath)
所以试试这个:
func setupHeader(collectionView: UICollectionView, headerForItemAtIndexPath indexPath: IndexPath) -> UICollectionReusableView
我希望这会有所帮助。