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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With