How to load custom cell ( xib) in UICollectionView cell using swift
Here is what you can do,
Change your
MyCustomView
class to be a subclass of UICollectionViewCell and not UIView.Remove
override init(frame : CGRect)
,required init?(coder aDecoder: NSCoder)
,func xibSetup()
,func loadViewFromNib() -> UIView
fromMyCustomView
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.
Finally you will be left with just IBOutlets in MyCustomView.
For better coding practice change the name from MyCustomView to MyCustomCell (optional)
Go to your xib, select the xib and set its class as MyCustomView.
- In the same screen change file owner to yourView controller hosting collectionView
- In ViewDidLoad of your viewController register your nib.
self.collectionView.registerNib(UINib(nibName: "your_xib_name", bundle: nil), forCellWithReuseIdentifier: "your_reusable_identifier")
- 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
- 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)
(2)
(3)
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
How to Set-Up in App Purchase Free Trial Period in iOS App
How to Get Image File Size in Swift
To Create a New Uiwindow Over the Main Window
Navigationview and Navigationlink on Button Click in Swiftui
Problems with Ssl Pinning and Afnetworking 2.5.0 (Nsurlerrordomain Error -1012.)
Prevent Duplicate Symbols When Building Static Library with Cocoapods
Detect If a Point Is Inside a Mkpolygon Overlay
Setting Alpha on Uiview Sets the Alpha on Its Subviews Which Should Not Happen
Linker Errors When Trying to Install New Google Analytics 3.0 Beta
How to Link Googleanalytics 3.01 with Xcode 5 (Missing Required Architecture X86_64)
How to Change the Highlighted Color of a Uibutton
Setting an Nsmanagedobject Relationship in Swift
Objective C - Pass by Value and Pass by Reference
How to Import Private Framework Headers in a Swift Framework