2016-04-03 47 views
0

我知道那里已经有问题了,但是我不理解他们,或者我只是不知道我在做什么。所以,当我尝试运行我的应用程序ViewView里面的CollectionView

断言失败中我得到这个错误 - [UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] 终止应用程序由于未捕获的异常“NSInternalInconsistencyException”,理由是:“不能出列类型的视图:UICollectionElementKindCell与标识符DateCell - 必须注册标识的笔尖或类或情节串连图板”

连接原型细胞,我不明白为什么我得到这个或如何解决它。

我有我已经注册

import Foundation 
import UIKit 

class CVCell: UICollectionViewCell { 
    @IBOutlet weak var myCellLabel: UILabel! 
} 

然后在这里一个小区类,是我的viewController:

import UIKit 

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {  

    private let reuseIdentifier = "DateCell" 

    override func viewDidLoad() { 
     initializeVars() 
     //moreDateInfo() 
    } 

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

    // make a cell for each cell index path 
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

     // get a reference to our storyboard cell 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CVCell 

     // Use the outlet in our custom class to get a reference to the UILabel in the cell 
     cell.myCellLabel.text = String(startOfMonthDate) 

     if (startOfMonthDate == numOfDaysInThisMonth) { 
      startOfMonthDate = 1 
     } else { 
      startOfMonthDate++ 
     } 

     //cell.backgroundColor = UIColor.yellowColor() // make cell more visible in our example project 
     cell.layer.borderColor = UIColor.blackColor().CGColor 
     cell.layer.borderWidth = 1 
     cell.layer.cornerRadius = 10 

     return cell 
    } 

    // MARK: - UICollectionViewDelegate protocol 

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
     // handle tap events 
     print("You selected cell #\(indexPath.item)!") 
    } 
    func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) { 
     let cell = collectionView.cellForItemAtIndexPath(indexPath) 
     cell?.backgroundColor = UIColor.blackColor() 
    } 


    // change background color back when user releases touch 
    func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath) { 
     let cell = collectionView.cellForItemAtIndexPath(indexPath) 
     cell?.backgroundColor = UIColor.whiteColor() 
    } 

这是我的故事板:

回答

1

这条线:

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CVCell 

实际上并没有注册CVCell类。要做到这一点,您应该在viewDidLoad中使用registerClass:forCellReuseIdentifier:。或者,在你的故事板选择单元格,并在正确的属性检查器中,输入“DATACELL”下的‘再利用标识符’。

0

哇,我有一个拼写错误.....永远不会忘记那些错别字。

private let reuseIdentifier = "DateCell" 

应该是 “DATACELL”

1

您的单元名称是错误的‘DateCell’

而且可能是你需要寄存器笔尖类像下面

self.collectionViewCategory.registerNib(UINib(nibName: “SelectCategoriesCell”,束:无),forCellWithReuseIdentifier:reuseIdentifier)

+0

并且最初可能需要注册nib类: – Anny

1

在我的故事板与原型细胞,它不要求注册该故事板处理该问题