UICollectionView using Swift

In my project, I created a cell in a UICollectionViewCell

Received application to complete errors due to an uncaught exception

The following code.

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 following code:

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 problem: I get the 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:**
+4
source share
4 answers

I added the following lines to NextViewController.swiftin the section viewDidLoad():

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

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

The problem was that I did not register anything. Now when I do this, it works fine.

+16
source

same for header / footer:

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

.

viewDidLoad.

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

dequeueReusableCellWithReuseIdentifier: forIndexPath: , registerNib: forCellWithReuseIdentifier: , . , .

nib , , cellClass, . nil cellClass, .

registerClass: forCellWithReuseIdentifier:

+2

, , - .

, registerNib:forCellWithReuseIdentifier: registerClass:forCellWithReuseIdentifier:. : Identity Inspector , . , . .

, , .

0

Source: https://habr.com/ru/post/1544267/


All Articles