显示节标题UICollectionReusableView [英] Display Section Header UICollectionReusableView
本文介绍了显示节标题UICollectionReusableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用iOS应用程序,但是在使用UICollectionView单元时遇到了一些问题.
I was working on iOS application and I have several problem about using UICollectionView cell.
这次,我想问一下如何显示UICollectionView(UICollectionReusableView)的节标题
This time, I want to ask about how to display the section header of UICollectionView (UICollectionReusableView)
我已经实现了如下功能:
I already implement the function like below :
public func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath as IndexPath)
var labelHeader = headerView.viewWithTag(2) as! UILabel
if indexPath.section == 0 {
labelHeader.text = "Specialist Clinic"
}
else {
labelHeader.text = "Medical Support"
}
headerView.backgroundColor = UIColor.blue;
return headerView
default:
assert(false, "Unexpected element kind")
}
}
但是,它总是给出空白结果.请看下面的屏幕截图
but, it always give a blank result. please look at the screen shot below
推荐答案
您需要返回标头的大小.
You need to return size of header .
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
referenceSizeForHeaderInSection section: Int) -> CGSize{
return CGSize(width: CGFloat(collectionView.frame.size.width, height: CGFloat(135)) // you can change here
}
代理方法
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
var reusableview: UICollectionReusableView? = nil
if kind == UICollectionElementKindSectionHeader {
reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath) // cellHea is your identifier
var labelHeader = reusableview.viewWithTag(2) as! UILabel
if indexPath.section == 0 {
labelHeader.text = "Specialist Clinic"
}
else {
labelHeader.text = "Medical Support"
}
headerView.backgroundColor = UIColor.blue;
}
return reusableview!
}
这篇关于显示节标题UICollectionReusableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文