How to Load Custom Cell ( Xib) in Uicollectionview Cell Using Swift

How to load custom cell ( xib) in UICollectionView cell using swift

Here is what you can do,

  1. Change your MyCustomView class to be a subclass of UICollectionViewCell and not UIView.

  2. Remove override init(frame : CGRect),required init?(coder aDecoder: NSCoder),func xibSetup(),func loadViewFromNib() -> UIView from MyCustomView

  3. I seriously could not understand how are you using your setter and getter for mytitleLabelText and myCustomImage. If its of no use get rid of it as well.

  4. Finally you will be left with just IBOutlets in MyCustomView.

  5. For better coding practice change the name from MyCustomView to MyCustomCell (optional)

  6. Go to your xib, select the xib and set its class as MyCustomView.

Sample Image


  1. In the same screen change file owner to yourView controller hosting collectionView

Sample Image


  1. In ViewDidLoad of your viewController register your nib.
self.collectionView.registerNib(UINib(nibName: "your_xib_name", bundle: nil), forCellWithReuseIdentifier: "your_reusable_identifier")

  1. In cellForItemAtIndexPath,
let cell : MyCustomView = collectionView.dequeueReusableCellWithReuseIdentifier("your_reusable_identifier", forIndexPath: indexPath) as! MyCustomView
cell.lblName.text = "bla bla" //access your Cell's IBOutlets
return cell

  1. Finally in order to control the size of cell either override the delegate of collectionView or simply go to your collectionView select the collectionCell in it and drag it to match your dimension :) Thats it :)

Happy coding. Search tutorials for better understanding. I can't explain all delegates as I'll end up writing a blog here.

Happy coding

Add cell to collection view. Swift

You need to mention your xib file name instead of "playerCellId"

let nibPlayersCell = UINib(nibName: playerCellId, bundle: nil)

It requires nibName = "YourNibName"

If your nib name is CollectionPlayersCell, then this should be like this -

let nibPlayersCell = UINib(nibName: "CollectionPlayersCell", bundle: nil)

How to create outlet from collection cell xib file to collection view controller

You can't do that.
You can't create an outlet that references a UITextField placed inside a custom CollectionViewCell in your CollectionViewController.

What you can do is create an outlet that references the UITextField in your .xib in the respective CollectionViewCell .swift class (1), create an outlet that references your collectionView in the CollectionViewController (2), and use these CollectionViewCells in your collectionView (3).

You can then access the UITextField of a specific CollectionViewCell in your CollectionViewController through the cell itself (3), like for instance with:

let indexPath = IndexPath(row: 2, section: 0)
let cell = self.collectionView.cellForItemAtIndexPath(indexPath) as! CollectionViewCell // so the compiler knows that the cell you are referring to is of type CollectionViewCell and thus has your custom textField property

let textInTextField = cell.textField.text

(1)

Sample Image

(2)

Sample Image

(3)

Sample Image

How to proper create a custom cell with a xib file, register, and load it ?

You need to set the xib file custom class to the class you cast it to

let cell = tableView.dequeueReusableCell(withIdentifier: "customPlaceDetailCell", for: indexPath)
as! CustomPlaceDetailCell

as it seems that xib name is PlaceDetailCell with class PlaceDetailCell and you cast it to CustomPlaceDetailCell

if you register this

 placesTable.register(UINib(nibName: "PlaceDetailCell", bundle: nil), forCellReuseIdentifier: "customPlaceDetailCell")

then the xib custom class should be set to CustomPlaceDetailCell not to PlaceDetailCell which is expected as always the xib name = it's class name that you create and then wanted to change the class name

Swift. How to create Segue from custom CollectionViewCell to .xib File (ViewController), by programmatically maybe?

You do not need segue you can pass data programmatically like this.

Let suppose your XIB and controller name is same.

let controller =  YourViewController()
controller.data = yourdatta
self.navigationController.push(controller)


Related Topics



Leave a reply



Submit