Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

ios - UICollectionView Using Swift

In My Project I created Cell in UICollectionViewCell

Its got Error Terminating app due to uncaught exception

The code as follow.

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:

The code as follow :

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:**
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...