【问题标题】:UICollectionView Using Swift使用 Swift 的 UICollectionView
【发布时间】:2014-06-12 05:26:31
【问题描述】:

在我的项目中,我在 UICollectionViewCell 中创建了 Cell

由于未捕获的异常而终止应用程序时出错

代码如下。

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:**

【问题讨论】:

    标签: ios iphone uicollectionview swift


    【解决方案1】:

    我在NextViewController.swiftviewDidLoad()下添加了以下两行:

    var nibName = UINib(nibName: "GalleryCell", bundle:nil)
    
    collectionView.registerNib(nibName, forCellWithReuseIdentifier: "CELL")
    

    问题是我没有注册笔尖。现在我这样做了,它工作正常。

    【讨论】:

      【解决方案2】:

      swift 中的页眉/页脚也是如此:

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

      【讨论】:

        【解决方案3】:

        你需要注册一个单元类。

        在你的viewDidLoad写下。

        collectionView.registerClass(NSClassFromString("GalleryCell"),forCellWithReuseIdentifier:"CELL");
        

        在调用之前 dequeueReusableCellWithReuseIdentifier:forIndexPath: 方法 集合视图,您必须使用此方法或 registerNib:forCellWithReuseIdentifier: 告诉集合的方法 查看如何创建给定类型的新单元格。如果一个细胞 指定类型当前不在重用队列中,集合视图 使用提供的信息创建一个新的单元格对象 自动。

        如果您之前注册了具有相同重用性的类或 nib 文件 标识符,您在 cellClass 参数中指定的类替换 旧条目。如果您愿意,可以为 cellClass 指定 nil 从指定的重用标识符中注销类。

        参考registerClass:forCellWithReuseIdentifier:

        【讨论】:

        • 由于未捕获的异常“NSInternalInconsistencyException”导致应用程序出现以下错误,原因:“尝试注册不是 UICollectionViewCell ((null)) 子类的单元类”
        • @PREMKUMAR:检查您的 GalleryCell 课程。您是否将其作为 UICollectionViewCell 的子类?问题出在日志本身
        • 谢谢我得到答复。感谢您花费宝贵的时间。
        • 我不得不这样说:self.collectionView.registerClass(CustomCollectionViewCell.self, forCellWithReuseIdentifier:reuseIdentifier)
        【解决方案4】:

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

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

        完成此操作并保存情节提要后,上述崩溃不再发生,我可以看到我的自定义标题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多