UICollectionView的自定义标头与xib失败

时间:2016-03-11 09:21:20

标签: ios objective-c swift

let identifier = "collection"
var collectionView:UICollectionView!
override func viewDidLoad() {
    super.viewDidLoad()
    loadNav()

    let layout = UICollectionViewFlowLayout()
    layout.minimumLineSpacing = 8
    layout.minimumInteritemSpacing = 8
    layout.itemSize = CGSizeMake(ScreenWidth / 2 - 12, 150)
    collectionView = UICollectionView(frame: CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64), collectionViewLayout: layout)
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.registerNib(UINib(nibName: "StoreCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: identifier)
    collectionView.registerNib(UINib(nibName: "HeadCollectionVIew", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "head")

    self.view.addSubview(collectionView)

}

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

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    let head = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "head", forIndexPath: indexPath)

    return head
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSizeMake(ScreenWidth, 280)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsMake(0, 8, 0, 8)
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! StoreCollectionViewCell


    return cell
}



import UIKit

class HeadCollectionVIew: UICollectionReusableView {

@IBOutlet weak var test: UIView!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

}

这是我的所有代码。我试图在名为HeadCollectionView的xib中添加一个空视图。当我运行我的应用程序时,它会崩溃,但是当我删除空视图时,它会起作用。

我收到以下错误:

Terminating app due to uncaught exception 'NSUnknownKeyException', 
reason: '[<NSObject 0x7fc98e089190> setValue:forUndefinedKey:]: 
this class is not key value coding-compliant for the key test.'

0 个答案:

没有答案