显示部分 Header UICollectionReusableView
Display Section Header UICollectionReusableView
我正在开发 iOS 应用程序,但在使用 UICollectionView 单元格时遇到了一些问题。
这次想问一下如何显示UICollectionView(UICollectionReusableView)header部分
我已经实现了如下功能:
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")
}
}
但是,它总是给出空白结果。请看下面的屏幕截图
您需要return尺寸header。
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: 123) // you can change sizing 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) // cellHeader 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!
}
您需要在 headerView 上添加 UILabel
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;
headerView.addSubview(labelHeader) //Add UILabel on HeaderView
return headerView
default:
assert(false, "Unexpected element kind")
}
}
我已经为您创建了演示。下载并重新使用到您的代码中。干杯!
下载 Link : https://www.dropbox.com/sh/vzf2tpe0ccf41tv/AABjdPAoaP2sE7YRtUgersq4a?dl=0
这是我的解决方案。它的完成类似于为 indexPathAt.
出列 cell/row
集合视图
// size of header
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
CGSize(width: collectionView.frame.size.width, height: 123)
}
// content of header
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
guard let headerCell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: CollectionViewHeader.identifier, for: indexPath) as? CollectionViewHeader else {
return UICollectionReusableView()
}
headerCell.titleLabel.text = "Title"
return headerCell
}
UICollectionReusableView 子类
class CollectionViewHeader: UICollectionReusableView {
let identifier = "headerView"
@IBOutlet weak var titleLabel: UILabel!
}
我正在开发 iOS 应用程序,但在使用 UICollectionView 单元格时遇到了一些问题。
这次想问一下如何显示UICollectionView(UICollectionReusableView)header部分
我已经实现了如下功能:
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")
}
}
但是,它总是给出空白结果。请看下面的屏幕截图
您需要return尺寸header。
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: 123) // you can change sizing 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) // cellHeader 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!
}
您需要在 headerView 上添加 UILabel
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;
headerView.addSubview(labelHeader) //Add UILabel on HeaderView
return headerView
default:
assert(false, "Unexpected element kind")
}
}
我已经为您创建了演示。下载并重新使用到您的代码中。干杯!
下载 Link : https://www.dropbox.com/sh/vzf2tpe0ccf41tv/AABjdPAoaP2sE7YRtUgersq4a?dl=0
这是我的解决方案。它的完成类似于为 indexPathAt.
出列 cell/row集合视图
// size of header
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
CGSize(width: collectionView.frame.size.width, height: 123)
}
// content of header
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
guard let headerCell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: CollectionViewHeader.identifier, for: indexPath) as? CollectionViewHeader else {
return UICollectionReusableView()
}
headerCell.titleLabel.text = "Title"
return headerCell
}
UICollectionReusableView 子类
class CollectionViewHeader: UICollectionReusableView {
let identifier = "headerView"
@IBOutlet weak var titleLabel: UILabel!
}