Register nib with collectionview

13,557

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.

Share:
13,557
SPatel
Author by

SPatel

Do lots of things to avoid lots of code

Updated on August 08, 2022

Comments

  • SPatel
    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
    SPatel over 6 years
    How to register AppFeedCell with collectionView
  • Dixit Akabari
    Dixit Akabari over 6 years
    @SPatel Go to your xib, select the xib and set its class as AppFeedCell.
  • SPatel
    SPatel over 6 years
    So i have to create new xib?
  • Priya
    Priya over 6 years
    No just set class type = AppFeedCell instead of FeedCell
  • SPatel
    SPatel over 6 years
    But i m also using FeedCell
  • SPatel
    SPatel over 6 years
    I did it for FeedCell
  • Dixit Akabari
    Dixit Akabari over 6 years
    Please tell what you want exactly.
  • Priya
    Priya over 6 years
    If you are subclassing FeedCell that means you are inheriting class to AppFeedCell where you can use all properties and methods of FeedCell.
  • SPatel
    SPatel over 6 years
    I want both cell in my collectionview
  • Dixit Akabari
    Dixit Akabari over 6 years
    but you can use AppFeedCell as class in FeedCell not UICollectionViewCell so you can use AppFeedCell as a subclass FeedCell.
  • SelvamSankarans
    SelvamSankarans over 6 years
    self.yourcollectionview.register(AppFeedCell.self, forCellReuseIdentifier: "your_reusable_identifier") // did you try this?
  • SPatel
    SPatel over 6 years
    So how to register and dequee botth cell?
  • Dixit Akabari
    Dixit Akabari over 6 years
    if you want in your collectionview then why its parent class is FeedCell?
  • SPatel
    SPatel over 6 years
    @SelvamSankaran it not work , i get all property nil
  • SPatel
    SPatel over 6 years
    @SelvamSankaran im using xib'
  • SPatel
    SPatel over 6 years
    i know that, but how to register and use both cell in same collectionview
  • Priya
    Priya over 6 years
    If 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
    Priya over 6 years
  • Dixit Akabari
    Dixit Akabari over 6 years
    @SPatel not create new bt use with if condition and user identifier are both same.