使用 Swift 的 UICollectionView [英] UICollectionView Using Swift
本文介绍了使用 Swift 的 UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的项目中,我在 UICollectionViewCell 中创建了 Cell
In My Project I created Cell in UICollectionViewCell
由于未捕获的异常而终止应用程序时出错
Its got Error Terminating app due to uncaught exception
代码如下.
GalleryCell.swift
class GalleryCell: UICollectionViewCell
{
@IBOutlet var titleLabel : UILabel
init(coder aDecoder: NSCoder!)
{
super.init(coder: aDecoder)
}
}
我在我的 ViewController 中使用了这个单元格:
and I used this cell in My ViewController:
代码如下:
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:**
推荐答案
我在viewDidLoad()下的
:NextViewController.swift
中添加了如下两行
I added the following two lines in NextViewController.swift
under 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.
这篇关于使用 Swift 的 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文