Uicollectionviewcell Register Class Fails, But Register Nib Works

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.

UICollectionViewCell unable to register nib?

u need to register tableview cell in viewcontroller and collectionview cell in awakefrom nib of table cell as follws

tableview.register(UINib(nibName: "nibname", bundle: nil), forCellWithReuseIdentifier: "identifier")

override func awakeFromNib() {
super.awakeFromNib()
collectionView.register(UINib(nibName: "collectioncellnibname", bundle: nil), forCellWithReuseIdentifier: "cvc_song")

}

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

UICollectionView, nib from a framework target registered as a cell fails at runtime

Solved.

TL:DR;
When changing target membership for files, remember to do Product > Clean before next build!

Explanation:
The problem was that "SomeUI" isn't a valid UIBundle identifier and so NSBundle(identifier: "SomeUI") returned nil, so the registerNib line registered a nib from the application bundle not the framework. The application bundle didn't contain the cell implementation class so it crashed.

But… the application bundle shouldn't have contained the nib either? So what was going on there? I had been switching the target membership for the nib and implementation file. I had not been running 'Clean' between builds, so a copy of the nib was left in the application target build folder.

The solution to original problem was to correctly load the nib from the SomeUI framework by replacing NSBundle(identifier: "SomeUI") with NSBundle(forClass: JamesTheCollectionViewCell.self)

IOS UICollectionView Register nib for UICollectionViewCell not working

photoCell is not registered.. is this the actual code you have? because you have a white space forCellWithReuseIdentifier: remove the white space..

Happy coding, Cheers!

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.

Swift - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

Change

self.collectionView.register(UICollectionCell.self, forCellReuseIdentifier: "Cell")

to

self.collectionView.register(UICollectionViewCell.self, forCellReuseIdentifier: "Cell")

And

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! UICollectionViewCell

What's the CollectionViewCell stands for? It's derived class or something?

Because you register UICollectionCell but call CollectionViewCell ?

I hope it works.

// edit

Final Solution:

class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var mainListButton: UIButton!
@IBOutlet weak var mainListLabel: UILabel!

override init(frame: CGRect) {
super.init(frame: frame)

self.backgroundColor = UIColor.black
}

required init?(coder: NSCoder) {
super.init(coder: coder)

self.backgroundColor = UIColor.black
}

}

Set your custom collection view class or use XIB.

Subclass of UICollectionViewCell Not Displaying

I checked your code. You have done perfectly. Collection view with cells is showing correctly, but you cannot see that since you are not setting any of the property of the cell. Just check by setting background color of the cell in cellForItem

  cell.backgroundColor = [UIColor redColor];

If you are done everything in nib then you need to register nib instead of class. use registerNib instead of registerClass. If you are registering class you have to do everything programmatically.



Related Topics



Leave a reply



Submit