NullHypothesis
NullHypothesis

Reputation: 4506

CollectionVIew not rendering cell when collection count is 1

I am facing a challenge where my UICollectionView does not show an item when the collection count is 1. More specifically, it flashes briefly when calling reloadSections or does not show at all when calling reloadData. This collectionview is present as an item inside a UITableViewCell.

Even though I am not using Xamarin, this post I found on a Xamarin board pretty much sums up what I am experiencing: https://forums.xamarin.com/discussion/35471/cells-from-custom-uicollectionviewcontroller-disappear-after-a-second (after 1 second, my cell vanishes)

Everything is extremely basic:

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
         return 1
     }
     
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.members.count  
     }
    
    
       //Rendering the collection view cells
       func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath)
               let member: GroupMemberDto = members[indexPath.item]
               let contactInitials = cell.viewWithTag(1) as! UILabel
               let contactAvatar = cell.viewWithTag(2) as! UIImageView
               contactAvatar.image = UIImage(named: "anonymous")
               contactInitials.text = member.displayName
               contactAvatar.layer.cornerRadius = contactAvatar.frame.size.width / 2
               contactAvatar.clipsToBounds = true
               contactAvatar.contentMode = UIViewContentMode.scaleAspectFill
               contactAvatar.layer.borderWidth = 0.2
               contactAvatar.layer.borderColor = UIColor.black.cgColor
   

               UserService.getProfilePicture(userId: member.userId) {
                       response in
                       contactAvatar.image = response
               }
           

           return cell
       }

When I bind it to a collection of 10 items, it works. If, however, I go into "numberOfItemsInSection" and change this to return "1" instead of "self.members.count", it flashes the first member briefly then just disappears.

I'm convinced the issue is not related to code rather the UI - I came across a few other posts that tangentially mentioned things will not draw out or these odd bugs can occur, however I wasn't able to cobble together a definitive answer.

Could anyone guide me or offer some suggestions on what might be causing this?

Thank you!!

Upvotes: 0

Views: 843

Answers (2)

Pedro Teloeken
Pedro Teloeken

Reputation: 1

I lost two hours in this problem and was solved using

collectionView.contentInset = .init(top: 1, left: 15, bottom: 1, right: 15)
    collectionView.scrollIndicatorInsets = .init(top: 1, left: 1, bottom: 1, right: 1)

Upvotes: 0

NullHypothesis
NullHypothesis

Reputation: 4506

Alright so I figured this out and hope it might help someone.

I went into the collectionview on the UIStoryBoard and set the content insets to 1 (previously they were 0) and in addition the scroll indicator insets to 1 as well (they were all 0).

After doing this, the item rendered and stayed rendered!

Upvotes: 2

Related Questions