Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register nib with collectionview

I have FeedCell.swift and FeedCell.xib, in feedcell xib i set cell custom class to 'FeedCell'

Code in view controller viewDidLoad

collectionView.register(UINib(nibName: "FeedCell", bundle: .main), forCellWithReuseIdentifier: "feedCell")

Problem:

I want to subclass FeedCell and use that class with collectionView Like:

class FeedCell:UICollectionViewCell {
    @IBOutlet weak var showImageView: UIImageView!
    @IBOutlet weak var showIconImageView: UIImageView!

}

class AppFeedCell: FeedCell { 
    override func awakeFromNib() {
       super.awakeFromNib()
       // configure cell
    }
}

How to register/use FeedCell and AppFeedCell with collectionview?

like image 378
SPatel Avatar asked Jan 31 '18 12:01

SPatel


People also ask

How do I register a collection view cell?

Use a cell registration to register cells with your collection view and configure each cell for display. You create a cell registration with your cell type and data item type as the registration's generic parameters, passing in a registration handler to configure the cell.

How do I register a collection view in Swift?

We can register the cell using its name and identifier. Write this into the “viewDidLoad()” of your “ViewController” class. Change your CollectionView delegate and data source method as follow to show data using the custom cell. Complete code snippet of your “ViewController” class should something like this.


2 Answers

If your AppFeedCell have a different UI Configuration, for example it does not bind some IBOutlets defined in FeedCell or it adds new ones not present in it's superclass, then you will have to register both Nibs (asuming you have two separated Nib files)

collectionView.register(UINib(nibName: "FeedCell", bundle: .main), forCellWithReuseIdentifier: "feedCell")
collectionView.register(UINib(nibName: "AppFeedCell", bundle: .main), forCellWithReuseIdentifier: "appFeedCell")

And then you will be able to dequeue each one when you need.

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:  NSIndexPath) -> UICollectionViewCell {
    if you_need_AppFeedCell {
        let cell : AppFeedCell = collectionView.dequeueReusableCellWithReuseIdentifier("appFeedCell", forIndexPath: indexPath) as! AppFeedCell
        return cell
    } else
        let cell : FeedCell = collectionView.dequeueReusableCellWithReuseIdentifier("feedCell", forIndexPath: indexPath) as! FeedCell
        return cell
    }
}

This way you may have two different Nib files even if AppFeedCell subclass FeedCell.

Otherwise, if both class and subclass share the same cell layout and outlets, then simply by casting the dequeued cell (FeedCell) to AppFeedCell should be enough, without registering another Nib, as Taras Chernyshenko pointed above.

like image 144
Esteban Vallejo Avatar answered Oct 11 '22 11:10

Esteban Vallejo


You don't need to register separate xib for AppFeedCell. You already registered nib to your collectionView.

Just dequeue cell and cast it to needed class in your cellForItemAtIndexPath.

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath 
    indexPath:  NSIndexPath) -> UICollectionViewCell {
if you_need_AppFeedCell {
    let cell : AppFeedCell = collectionView.dequeueReusableCellWithReuseIdentifier("feedCell", forIndexPath: indexPath) as! AppFeedCell
    return cell
} else
    let cell : FeedCell = collectionView.dequeueReusableCellWithReuseIdentifier("feedCell", forIndexPath: indexPath) as! FeedCell
    return cell
}
}

Your AppFeedCell will inherit all outlets from FeedCell so it should work as well.

like image 34
Taras Chernyshenko Avatar answered Oct 11 '22 11:10

Taras Chernyshenko