Swift 将自定义标题添加到集合视图中
我正在尝试使用自定义xib文件将标题添加到Swift 将自定义标题添加到集合视图中,swift,uicollectionview,uicollectionreusableview,Swift,Uicollectionview,Uicollectionreusableview,我正在尝试使用自定义xib文件将标题添加到collectionView。我创建了xib文件,其中类实现了UICollectionReusableView。 在collectionViewController中,我注册了xib文件,如下所示: self.collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollecti
collectionView
。我创建了xib
文件,其中类实现了UICollectionReusableView
。
在collectionViewController
中,我注册了xib
文件,如下所示:
self.collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HCollectionReusableView.reuseIdentifier)
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView", for: indexPath) as! HCollectionReusableView
reusableview.frame = CGRect(0 , 0, self.view.frame.width, headerHight)
//do other header related calls or settups
return reusableview
default: fatalError("Unexpected element kind")
}
}
extension UIViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 100) //add your height here
}
}
之后,我在视图中查看了类似的辅助元素
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HCollectionReusableView.reuseIdentifier, for: indexPath) as! HCollectionReusableView
和上浆
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: 100, height: 50)
}
我收到错误:无法在捆绑中加载NIB。
有丢失的代码吗
HCollectionReusableView类:
class HCollectionReusableView: UICollectionReusableView {
static var nibName : String
{
get { return "headerNIB"}
}
static var reuseIdentifier: String
{
get { return "headerCell"}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}您是否在xib文件中设置了文件所有者设置?将文件所有者更改为承载collectionView的视图控制器
您需要调用视图,以获得类似的SupplementalElement
:
self.collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HCollectionReusableView.reuseIdentifier)
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView", for: indexPath) as! HCollectionReusableView
reusableview.frame = CGRect(0 , 0, self.view.frame.width, headerHight)
//do other header related calls or settups
return reusableview
default: fatalError("Unexpected element kind")
}
}
extension UIViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 100) //add your height here
}
}
这样,您可以初始化并显示标题
设置UICollectionViewHeader框架的另一种方法是扩展UICollectionViewDelegateFlowLayout
,如下所示:
self.collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HCollectionReusableView.reuseIdentifier)
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView", for: indexPath) as! HCollectionReusableView
reusableview.frame = CGRect(0 , 0, self.view.frame.width, headerHight)
//do other header related calls or settups
return reusableview
default: fatalError("Unexpected element kind")
}
}
extension UIViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 100) //add your height here
}
}
这样就不需要调用:
reusableview.frame = CGRect(0 , 0, self.view.frame.width, headerHight)
在上述情况下
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
在初始化UICollectionView
后,请记住通过调用以下命令注册HeaderView:
Swift 4.1更新
UICollectionElementKindSectionHeader
已重命名为UICollectionView.elementKindSectionHeader
是的,在文件所有者和集合可重用视图中,我想我必须将xib的注册更改为类,现在出现的错误是:无法将具有标识符HCollectionReusableView的UICollectionElementKindSectionHeader类型的视图出列-必须为标识符注册nib或类,或连接序列图像板中的原型单元(null)。您会收到错误,因为当您取消对项目的命名时,以及当您注册时,标识符不匹配。。。当您注册单元格时,尝试像这样硬编码标识符:self.collectionView.register(UINib(nibName:HCollectionReusableView.nibName,bundle:nil),对于种类:uicollectionlementkindsectionheader的SupplementViewofKind,使用reuseIdentifier:“HCollectionReusableView”)
这应该可以。。。然后,您可以调整代码以满足您的需要。您将仅在不使用Xib或故事板的情况下注册类。太好了,省省我的时间。您仍然需要注册您的类,而不是nib,否则它不会键入cast。yourCollectionView.register(YourClassName.self,用于种类的SupplmentViewofKind:UICollectionView.elementKindSectionHeader,带有ReuseIdentifier:“YourHardCodeIdentifierString”)
final class MyCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: MyCollectionReusableView.reuseIdentifierHeader)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 40)
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: MyCollectionReusableView.reuseIdentifierHeader, for: indexPath) as! MyCollectionReusableView
header.setupView()
return header
}
}
final class MyCollectionReusableView: UICollectionReusableView {
static let reuseIdentifierHeader = "MyId"
func setupView() {
//Code...
}
}