Ios 如何在UITableViewCell子类中注册UICollectionViewCell、HeaderCollectionReusableView?

Ios 如何在UITableViewCell子类中注册UICollectionViewCell、HeaderCollectionReusableView?,ios,uitableview,swift3,uicollectionview,Ios,Uitableview,Swift3,Uicollectionview,我制作了一个UITableViewCell子类 import UIKit class CategoryRow : UITableViewCell { @IBOutlet weak var collectionView: UICollectionView! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelecte

我制作了一个UITableViewCell子类

import UIKit

class CategoryRow : UITableViewCell {
    @IBOutlet weak var collectionView: UICollectionView!

  override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
  }

  override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
  }
}

extension CategoryRow : UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! VideoCell
        cell.backgroundColor = .red
        return cell
    }

  func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    switch kind {

    case UICollectionElementKindSectionHeader:
      let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderCollectionReusableView", for: indexPath)
      headerView.backgroundColor = UIColor.blue;
      return headerView

    case UICollectionElementKindSectionFooter:
      let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderCollectionReusableView", for: indexPath) 

      footerView.backgroundColor = UIColor.green;
      return footerView

    default:

      assert(false, "Unexpected element kind")
    }
  }

}

extension CategoryRow : UICollectionViewDelegateFlowLayout {

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width:collectionView.frame.size.width, height:30.0)
  }
}
现在当我运行这个代码时,我会崩溃

2017-03-19 12:41:43.298 DemoProject[15229:156486] *** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/UICollectionView.m:4971
2017-03-19 12:41:43.306 DemoProject[15229:156486] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindSectionHeader with identifier HeaderCollectionReusableView - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
我很清楚这是因为单元格或视图未注册。它是锡伯银行制造的

在UIViewController中,我们在ViewDidLoad中注册,所以它可以工作。同样,哪种方法是注册nib的理想方法

我试着输入代码

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
  }

有谁能告诉我在UITableViewCell中注册单元格的正确方法吗?

在您的
类别Row
中,尝试以下方法:

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    collectionView?.register(UINib(nibName: "<Nib Name>", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCollectionReusableView")
}
重写函数awakeFromNib(){
super.awakeFromNib()
//初始化代码
collectionView?.register(UINib(nibName:,bundle:nil),用于种类为UICollectionElementKindSectionHeader的SupplementView,带有ReuseIdentifier:“HeaderCollectionReusableView”)
}

在您的
类别Row
类中,尝试以下操作:

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    collectionView?.register(UINib(nibName: "<Nib Name>", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCollectionReusableView")
}
重写函数awakeFromNib(){
super.awakeFromNib()
//初始化代码
collectionView?.register(UINib(nibName:,bundle:nil),用于种类为UICollectionElementKindSectionHeader的SupplementView,带有ReuseIdentifier:“HeaderCollectionReusableView”)
}
试试这个:

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    collectionView?.register(UINib(nibName: "<Nib Name>", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCollectionReusableView")
}
UITableViewCell
子类的
awakeFromNib()
中注册集合视图单元格和补充视图

override func awakeFromNib()
{
    super.awakeFromNib()
    collectionView.register(UINib(nibName:"Your_Nib_Name"), forCellWithReuseIdentifier: "videoCell")
    collectionView.register(UINib(nibName:"Your_Nib_Name"), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCollectionReusableView")
    collectionView.register(UINib(nibName:"Your_Nib_Name"), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "HeaderCollectionReusableView")
}
试试这个:

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    collectionView?.register(UINib(nibName: "<Nib Name>", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCollectionReusableView")
}
UITableViewCell
子类的
awakeFromNib()
中注册集合视图单元格和补充视图

override func awakeFromNib()
{
    super.awakeFromNib()
    collectionView.register(UINib(nibName:"Your_Nib_Name"), forCellWithReuseIdentifier: "videoCell")
    collectionView.register(UINib(nibName:"Your_Nib_Name"), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCollectionReusableView")
    collectionView.register(UINib(nibName:"Your_Nib_Name"), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "HeaderCollectionReusableView")
}