Uicollectionview's Cell Registerclass in Swift

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

  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.

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

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



Leave a reply



Submit