2017-03-19 5 views
2

私はUITableViewCell子クラスを作った。UITableViewCellサブクラスでUICollectionViewCell、HeaderCollectionReusableViewを登録するには?

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' 

を取得し、私はよく、細胞またはビューが登録されていないので、それがあることを知っています。そのxibによって作られた。

UIViewControllerでは、ViewDidLoadで登録するので機能します。同様に、どの方法がnibを登録するのに理想的です。

私は

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

にコードを入れてみましいずれかが私のUITableViewCell内のセルを登録するための正しい方法を教えてください。あなたのCategoryRowクラスで

答えて

0

これを試してみてください:

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") 
} 
0

、これを試してみてください:

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
    collectionView?.register(UINib(nibName: "<Nib Name>", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCollectionReusableView") 
} 
関連する問題