Raj NT
Raj NT

Reputation: 61

How to implement Custom UICollectionViewCell using Swift in iOS

Can anybody give me a right direction on, how to create custom UICollectionview cell in iOS. I have worked the same concept using Objective C to create an app, but with Swift I can't able to make it though. While I tried to run my application, I am getting the following error : "fatal error: Can't unwrap Optional.None"

func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
{
    var cell = collectionView?.dequeueReusableCellWithReuseIdentifier("CustomsCollectionViewCell", forIndexPath: indexPath) as CustomsCollectionViewCell
    var values = String(indexPath.row)
    cell.lblTextDisplay.text = values  **//// I got error in this line ////**

    return cell
}

If anyone provides any tutorial link that would be very helpful. Thanks in advance.

Upvotes: 1

Views: 2096

Answers (1)

Raj NT
Raj NT

Reputation: 61

Finally I got the solution for my problem, I am explaining how I resolved this below, instead of using collectionView.registerClass use:

let nibName=UINib(nibName: "CustomsCollectionViewCell", bundle:nil)
self.collectionView.registerNib(nibName, forCellWithReuseIdentifier: "CustomsCollectionViewCell")

Now it's working fine like a charm.

Upvotes: 4

Related Questions