UICollectionView's cell registerClass in Swift
This is currently just a blind but educated guess, but using Class.self
might be what you want.
collectionView.registerClass(MyCoolViewCell.self, forCellWithReuseIdentifier: "MyCoolViewCell")
UICollectionViewCell register class fails, but register nib works
I see this link Overview Collection View
If the cell class was written in code, the registration is performed using the registerClass: method of UICollectionView. For example:
[self.myCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"MYCELL"];
In the event that the cell is contained within an Interface Builder NIB file, the registerNib: method is used instead.
So your collection cell create with nib, you should register with nib. If your cell written totally in code you will need register with class.
Hope this help.
What's wrong with register(_:forCellWithReuseIdentifier:) on UICollectionView?
There are 3 ways to register a cell (either for UITableView
or UICollectionView
).
You can register a class. That means the cell has no nib or storyboard. No UI will be loaded and no outlets connected, only the class initializer is called automatically.
You can register a
UINib
(that is, axib
file). In that case the cell UI is loaded from the xib and outlets are connected.You can create a prototype cell in the storyboard. In that case the cell is registered automatically for that specific
UITableViewController
orUICollectionViewController
. The cell is not accessible in any other controller.
The options cannot be combined. If you have a cell prototype in the storyboard, you don't have to register again and if you do, you will break the storyboard connection.
UICollectionView registerClass: forCellWithReuseIdentifier method breaks UICollectionView
If you've already created your UICollectionView in Storyboard, connected your dataSource
and delegate
, and you have added all of the required methods:
numberOfItemsInSection
(not a required method - refer to this comment)numberOfSectionsInCollectionView
cellForItemAtIndexPath
Then the registerClass
/ registerCell
method isn't required. However, if you need to reuse a view, data, or cells then you should use those methods so that iOS can populate your UICollectionView as needed. This can also be done in your Storyboard by setting the Prototype Cell (the same principle as the registerClass
method).
Also, if you're looking for a good explanation on what registerCell
does and how to use it check out this link and scroll to the bottom section titled "Cell and View Reuse."
Attempt to register a cell class which is not a subclass of UICollectionViewCell. Huh?
There is no need to register the class if the prototype is designed in InterfaceBuilder. Just give it an reuse identifier there.
Registering a lot of UICollectionViewCell classes for reuse in a UICollectionView
Registering before dequeueing is likely very inefficient, so I would not recommend that out of the options you have.
Although I don't have true diagnostics to show it, registering every possible cell class in viewDidLoad
should not greatly impact performance, since this is what Apple recommends.
You can make separate methods to register particular cell classes depending on the class of the data source. However, in the grand scheme of things, this is not something that you should want to focus on if you want to improve performance or speed.
UICollectionView Using Swift
I added the following two lines in NextViewController.swift
under viewDidLoad()
:
var nibName = UINib(nibName: "GalleryCell", bundle:nil)
collectionView.registerNib(nibName, forCellWithReuseIdentifier: "CELL")
The problem was that I was not registering the nib. Now that I'm doing that, it's working fine.
UICollection view register class not work
You have a mistypo ("firendCell" and "friendCell")
let cell = friendsCollection?.dequeueReusableCellWithReuseIdentifier("firendCell", forIndexPath: indexPath) as! FriendCollectionViewCell
should be
let cell = friendsCollection?.dequeueReusableCellWithReuseIdentifier("friendCell", forIndexPath: indexPath) as! FriendCollectionViewCell
Related Topics
Why Push Notifications Is Not Working on Testflight
How to Change the Tint Color of the Clear Button on a Uitextfield
Why Does My Programmatically Created Screenshot Look So Bad on iOS 7
Uitableviewcell Checkmark to Be Toggled on and Off When Tapped
Find the Tangent of a Point on a Cubic Bezier Curve
Distancefromlocation - Calculate Distance Between Two Points
Localizing Strings in iOS: Default (Fallback) Language
How to Add an Action to a Button Programmatically in Xcode
iOS 11 - Keyboard Height Is Returning 0 in Keyboard Notification
How to Play Mp3 Audio from Url in iOS Swift
Missing Cfbundleiconname in Xcode9 iOS11 App Release
Remove Single Remote Notification from Notification Center
Ios: Frame.Size.Width/2 Doesn't Produce a Circle on Every Device
iOS 7 Uiwebview Keyboard Issue
Color All Occurrences of String in Swift
Exc_Bad_Instruction When Passing Uicollectionview Cell Data to Different Viewcontroller
Cocoapods: Failed to Connect to Github to Update the Cocoapods/Specs Specs Repo