0
votes

I'm having trouble dequeuing more than one cell in my uicollectionview. Can someone explain the process step by step from registration to dequeuing. I can't find anything online pertaining to loading two or more uicollectionview cells in one collection view. Again this is for loading more than one type of cell in a single collectionview based of a variable from coreadata.

here's what I have so far.

Here's where I register the cells

    collectionView?.register(ShareCell.self, forCellWithReuseIdentifier: cellId)
    collectionView?.register(ShareCellMedia.self, forCellWithReuseIdentifier: mediaCellId)

Here's my cell for item at index path

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath :
    IndexPath) -> UICollectionViewCell {

    let friend = fetchedResultsController.object(at: indexPath) as! Friend
    if (friend.lastMessage?.hasImage)! == true {
        let mediaCell = collectionView.dequeueReusableCell(withReuseIdentifier: mediaCellId, for: indexPath) as!
        ShareCellMedia
        mediaCell.cell = friend.lastMessage
        return mediaCell

    }else{


        let regularCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as!
        ShareCell
        regularCell.cell = friend.lastMessage
        return regularCell
    }

}
2
The code you posted seems fine. What is happening that you didn't expect to happen? are you sure that cellId and mediaCellId are set before using them? when are registering the cells, are you sure that collectionView is not nil at that point? - Jon Rose

2 Answers

0
votes

if you are using a prototype static cell no need to register the cell or if you are using a cell with xib you can register like this

     collectionView.register(UINib(nibName: "name here",bundle:nil), forCellWithReuseIdentifier: "identifier")

and also check or print your identifiers

0
votes

Are you creating your cells with xibs ??

if so then you have to register the nib cell in viewdidload like this

 UINib *cellNib = [UINib nibWithNibName:@"yourCellNib" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"TheNibCellIdentifier"];

Also you have to make sure to use the respective ReuseIdentifier when loading the cell:

yourCellNib*cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TheNibCellIdentifier"  forIndexPath:indexPath];