我有一个带有多个标题的UICollectionView
。
它们作为节标题实现。
每个 header 都属于其自己的 Controller ,并且这些 Controller 符合 MyHeaderProtocol
。
基本上,我有一个 Controller 数组,并在必要时向每个 Controller 请求 header 单元格。
所以:
collectionView(:viewForSupplementaryElementOfKind:at)
被调用并返回实际 View 。 View 设置正确,具有良好的帧大小等。以防万一我将 View 颜色设置为紫色 - 但我在屏幕上看不到它。collectionView(_:layout:referenceSizeForHeaderInSection:)
被调用并返回良好的 CGSize 值。
显示了 Collection View ,但我看到的不是所有这些标题,而是空白区域。 所以问题是 - 调用了显示标题部分的两种必要方法,但未显示部分标题(但为其保留了空间)。
这是我的代码的简化版本:
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
}
}
最佳答案
我认为问题可能出在您的自定义 MyHeaderProtocol
协议(protocol)上。该功能特别是:
func setupHeader(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: IndexPath) -> UICollectionViewCell?
您必须在此处返回 header ,否则此函数collectionView(:viewForSupplementaryElementOfKind:at
将不会产生预期的行为:
var view : UICollectionReusableView?
view = header.setupHeader(collectionView: collectionView, cellForItemAtIndexPath: indexPath)
所以试试这个:
func setupHeader(collectionView: UICollectionView, headerForItemAtIndexPath indexPath: IndexPath) -> UICollectionReusableView
我希望这会有所帮助。
关于ios - UIViewController 部分标题未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46492624/