Uicollectionview Registerclass: Forcellwithreuseidentifier Method Breaks Uicollectionview

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
  • numberOfSectionsInCollectionView (not a required method - refer to this comment)
  • 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).

  1. 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.

  2. You can register a UINib (that is, a xib file). In that case the cell UI is loaded from the xib and outlets are connected.

  3. You can create a prototype cell in the storyboard. In that case the cell is registered automatically for that specific UITableViewController or UICollectionViewController. 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



Leave a reply



Submit