ios 使用 Swift 的 UICollectionView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24177020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:51:01  来源:igfitidea点击:

UICollectionView Using Swift

iosiphoneuicollectionviewswift

提问by PREMKUMAR

In My Project I created Cell in UICollectionViewCell

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

Its got Error Terminating app due to uncaught exception

由于未捕获的异常,它得到了错误终止应用程序

The code as follow.

代码如下。

GalleryCell.swift

GalleryCell.swift

class GalleryCell: UICollectionViewCell
{


@IBOutlet var titleLabel : UILabel


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

and I used this cell in My ViewController:

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

The code as follow :

代码如下:

NextViewController.swift

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.
}

}

}

My issue is I am getting following error:

我的问题是我收到以下错误:

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

回答by PREMKUMAR

I added the following two lines in NextViewController.swiftunder viewDidLoad():

我在NextViewController.swiftunder添加了以下两行viewDidLoad()

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

collectionView.registerNib(nibName, forCellWithReuseIdentifier: "CELL")

The problem was that I was not registering the nib. Now that I'm doing that, it's working fine.

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

回答by ing.conti

the same for header/footer in swift:

swift中的页眉/页脚相同:

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

回答by Midhun MP

You need to register a cell class.

您需要注册一个单元类。

In your viewDidLoadwrite.

在你viewDidLoad写的。

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

Prior to calling the dequeueReusableCellWithReuseIdentifier:forIndexPath:method of the collection view, you must use this method or the registerNib:forCellWithReuseIdentifier: method to tell the collection view how to create a new cell of the given type. If a cell of the specified type is not currently in a reuse queue, the collection view uses the provided information to create a new cell object automatically.

If you previously registered a class or nib file with the same reuse identifier, the class you specify in the cellClass parameter replaces the old entry. You may specify nil for cellClass if you want to unregister the class from the specified reuse identifier.

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

如果您之前使用相同的重用标识符注册了一个类或 nib 文件,则您在 cellClass 参数中指定的类将替换旧条目。如果您想从指定的重用标识符中取消注册类,您可以为 cellClass 指定 nil。

Reference registerClass:forCellWithReuseIdentifier:

参考registerClass:forCellWithReuseIdentifier:

回答by pohl

I wanted to add another answer that helped me, in case it becomes useful to someone else.

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

It seems that everyone else has solved this problem through the use of registerNib:forCellWithReuseIdentifier:or registerClass:forCellWithReuseIdentifier:. For me, there was a different resolution: in my storyboard, I selected the header and looked over in the Identity Inspector in the top section where it has fields for my Custom Class. Specifically, the Modulefield was blank. I needed to pull down the arrow next to that field and select the right target.

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

Once I did this & saved the storyboard, the above crash no longer happened and I could see my custom header.

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