使用Swift的UICollectionView

4

在我的项目中,我创建了一个UICollectionViewCell中的单元格。

它出现了错误,应用程序终止了,因为出现了未捕获的异常。

代码如下:

GalleryCell.swift

class GalleryCell: UICollectionViewCell
{


@IBOutlet var titleLabel : UILabel


init(coder aDecoder: NSCoder!)
{
    super.init(coder: aDecoder)
}
}

我在我的ViewController中使用了这个单元格:

代码如下:

NextViewController.swift

import UIKit

 class NextViewController: UIViewController
 {

@IBOutlet var collectionView : UICollectionView



var ListArray=NSMutableArray()



override func viewDidLoad()
{
    super.viewDidLoad()


    for i in 0..70
    {
         ListArray .addObject("C: \(i)")
    }




}



   func collectionView(collectionView: UICollectionView, numberOfItemsInSection section:Int)->Int
    {
        return ListArray.count
   }


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell
{

  var  cell = collectionView.dequeueReusableCellWithReuseIdentifier("CELL", forIndexPath: indexPath) as GalleryCell


    cell.titleLabel.text="\(ListArray.objectAtIndex(indexPath.item))"

    return cell
}


func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
{

    return CGSizeMake(66, 58)
}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

我的问题是我遇到了以下错误:

解决方案:

***** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier CELL - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'*** First throw call stack:**
4个回答

20

我在NextViewController.swift文件的viewDidLoad()方法下添加了以下两行代码:

var nibName = UINib(nibName: "GalleryCell", bundle:nil)

collectionView.registerNib(nibName, forCellWithReuseIdentifier: "CELL")
问题在于我没有注册nib。现在我已经做到了,它运行良好。

5

在Swift中,页眉/页脚的处理方式相同:

    // register header accessory view:
    self.collectionView.registerClass(UICollectionReusableView.self,
        forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,
        withReuseIdentifier: headerReuseIdentifier);

2
您需要注册一个单元格类。
在您的viewDidLoad方法中写入。
collectionView.registerClass(NSClassFromString("GalleryCell"),forCellWithReuseIdentifier:"CELL");

在调用collection view的dequeueReusableCellWithReuseIdentifier:forIndexPath:方法之前,你必须使用registerClass:forCellWithReuseIdentifier:方法或registerNib:forCellWithReuseIdentifier:方法告诉collection view如何创建给定类型的新单元格。如果指定类型的单元格当前不在重用队列中,collection view将使用提供的信息自动创建新单元格对象。
如果你之前使用相同的重用标识符注册了一个类或nib文件,则cellClass参数中指定的类将替换旧条目。如果要注销指定重用标识符中的类,则可以将cellClass指定为nil。
参考registerClass:forCellWithReuseIdentifier:

出现以下错误:终止应用程序,原因是未捕获的异常 'NSInternalInconsistencyException',原因是尝试注册一个不是 UICollectionViewCell 的子类的单元格类 ((null))。 - PREMKUMAR
@PREMKUMAR:请检查您的GalleryCell类。您是否将其设置为UICollectionViewCell的子类?问题就在日志中。 - Midhun MP
谢谢,我已经得到答案了。感谢您花费宝贵的时间。 - PREMKUMAR
我必须这样声明: self.collectionView.registerClass(CustomCollectionViewCell.self, forCellWithReuseIdentifier:reuseIdentifier) - Eric_WVGG

0

我想再添加一个帮助我的答案,以防它对其他人有用。

似乎每个人都通过使用registerNib:forCellWithReuseIdentifier:registerClass:forCellWithReuseIdentifier:来解决这个问题。 对于我来说,有一个不同的解决方案:在我的Storyboard中,我选择了标题并在Identity Inspector中查看了顶部部分,在那里有我的自定义类字段。 具体来说,Module字段为空。 我需要拉下该字段旁边的箭头并选择正确的目标。

一旦我这样做并保存Storyboard,上面的崩溃就不会再发生了,我可以看到我的自定义标题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接