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."
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.
iOS SIGABRT on dequeueReusableCellWithReuseIdentifier
one cell identifier is "Cell" & another one is "Cell ". i.e. extra space after cell, remove it.
Crash when loading UICollectionView in, why?
Agree with the two solution below, I registered cell incorrectly, but afterward I found an other issue, cell's outlets become nil. And the solution for that was to remove whole registration, if UICollectionViewController was created via Interface Builder.
How do I set a different height for each UICollectionViewCell
Use this method
func heightForComment(comment:NSString,font: UIFont, width: CGFloat) -> CGFloat {
let rect = NSString(string: comment).boundingRectWithSize(CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return ceil(rect.height)
}
UICollectionView Crashes when Cell is Touched
You said you app will crash when cell is touched but you haven't implemented the cell select delegate method but written only cell deselect delegate method. Try this
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
And insert a breakpoint inside the same method to debug. If break point is not called then something else is the problem
Related Topics
Should I Git Ignore Xcodeproject/Project.Pbxproj File
React Native iOS Build:Can't Find Node
Watch Os 2.0 Beta: Access Heart Beat Rate
Editing Screenshots in Itunes Connect After iOS App Was Approved
Atomic Properties VS Thread-Safe in Objective-C
How to Cache Resources Loaded in an iPhone Uiwebview
Text Color Based on Background Image
What Exactly Does 'Pod Repo Update' Do
What Do Horizontalaccuracy and Verticalaccuracy of a Cllocation Refer To
Firebase: Cannot Upload Production Apns Certificate
Avcapturevideopreviewlayer Orientation - Need Landscape
How to Disable Calayer Implicit Animations
How to Apply Filter to Video Real-Time Using Swift
How to Delete App Build in New Itunes Connect Site
Nstimer - How to Delay in Swift
Create and Store PDF Document Programmatically Using Swift for iOS
Implementing Auto Layout for Views Generated Programmatically