将自定义标题添加到集合视图swift

Kha*_*yek 8 uicollectionview uicollectionreusableview swift

我正在尝试添加标头以collectionView使用自定义xib文件.我xib用类实现创建了文件UICollectionReusableView.在collectionViewController我注册的xib文件是这样的:

self.collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HCollectionReusableView.reuseIdentifier)
Run Code Online (Sandbox Code Playgroud)

在那之后,viewForSupplementaryElementOfKind我做到了

let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HCollectionReusableView.reuseIdentifier, for: indexPath) as! HCollectionReusableView
Run Code Online (Sandbox Code Playgroud)

和尺寸

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: 100, height: 50)
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:无法在捆绑中加载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
}
Run Code Online (Sandbox Code Playgroud)

}

anh*_*nho 25

你需要viewForSupplementaryElementOfKind像这样打电话:

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")
    }
}
Run Code Online (Sandbox Code Playgroud)

这样您就可以初始化并显示标题

设置UICollectionViewHeader框架的另一种方法是通过这样扩展UICollectionViewDelegateFlowLayout:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

这消除了调用的需要:

reusableview.frame = CGRect(0 , 0, self.view.frame.width, headerHight)
Run Code Online (Sandbox Code Playgroud)

在上面提到的

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
Run Code Online (Sandbox Code Playgroud)

记得UICollectionView通过调用初始化后注册HeaderView :

collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView")
Run Code Online (Sandbox Code Playgroud)

Swift 4.1更新

UICollectionElementKindSectionHeader 已重命名为 UICollectionView.elementKindSectionHeader

  • 您会收到错误,因为当您将项目出队时和注册时的标识符不匹配...尝试在注册单元格时对标识符进行硬编码: `self.collectionView.register(UINib(nibName: HCollectionReusableView.nibName) ,bundle:nil),forSupplementaryViewOfKind:UICollectionElementKindSectionHeader,withReuseIdentifier:“HCollectionReusableView”)`这应该可以工作...然后您可以调整代码以满足您的需求 (2认同)

小智 5

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...
   }

}
Run Code Online (Sandbox Code Playgroud)