Register nib with collectionview
Solution 1
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.
Solution 2
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.
Comments
-
SPatel over 1 year
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?
-
SPatel over 6 yearsHow to register AppFeedCell with collectionView
-
Dixit Akabari over 6 years@SPatel Go to your xib, select the xib and set its class as AppFeedCell.
-
SPatel over 6 yearsSo i have to create new xib?
-
Priya over 6 yearsNo just set class type = AppFeedCell instead of FeedCell
-
SPatel over 6 yearsBut i m also using FeedCell
-
SPatel over 6 yearsI did it for FeedCell
-
Dixit Akabari over 6 yearsPlease tell what you want exactly.
-
Priya over 6 yearsIf you are subclassing FeedCell that means you are inheriting class to AppFeedCell where you can use all properties and methods of FeedCell.
-
SPatel over 6 yearsI want both cell in my collectionview
-
Dixit Akabari over 6 yearsbut you can use AppFeedCell as class in FeedCell not UICollectionViewCell so you can use AppFeedCell as a subclass FeedCell.
-
SelvamSankarans over 6 yearsself.yourcollectionview.register(AppFeedCell.self, forCellReuseIdentifier: "your_reusable_identifier") // did you try this?
-
SPatel over 6 yearsSo how to register and dequee botth cell?
-
Dixit Akabari over 6 yearsif you want in your collectionview then why its parent class is FeedCell?
-
SPatel over 6 years@SelvamSankaran it not work , i get all property nil
-
SPatel over 6 years@SelvamSankaran im using xib'
-
SPatel over 6 yearsi know that, but how to register and use both cell in same collectionview
-
Priya over 6 yearsIf you want to use both cell then you need to create separate xib for AppFeedCell and need to register 2 cells. And you can use these cells with
if {...)else{...}
-
Priya over 6 yearsLet us continue this discussion in chat.
-
Dixit Akabari over 6 years@SPatel not create new bt use with if condition and user identifier are both same.