Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Do My UICollectionViewCells Disappear After Scroll?

I am using a UICollectionView with a custom layout, but I am having issues with cells disappearing after I scroll down and back up.

Please see a video demonstration of my issue.

I've done some Googling around and other people have related issues and I recognise that this is possibly due to cell reuse, but none of the answers I found elsewhere helped me.

So my questions are:

  1. Why is this happening?

  2. How do I stop this from happening?

Interestingly, once I present a UIViewController and dismiss it, the issue no longer occurs. Please see the video of this in action.

NOTE: This bug has been here for a while (at least since iOS 10 and hasn't been fixed in iOS 11 beta 1 either).

Edit 1

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    switch (indexPath as NSIndexPath).row {
    case 0:
        // Speak Logo Cell
        let speakLogoCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SpeakLogoCell", for: indexPath) as! SpeakLogoCell

        recordButton = speakLogoCell.recordButton

        return speakLogoCell
    case 1:
        // Text Input Cell
        let inputCell = collectionView.dequeueReusableCell(withReuseIdentifier: "InputCell", for: indexPath) as! InputCell

        inputCell.inputTextView.delegate = self
        inputTextView = inputCell.inputTextView

        // Only restore input if launching and required.
        if currentlyLaunching && UserDefaultsHelper.loadShouldRestoreInput() {
            inputTextView!.text = UserDefaultsHelper.loadInput() ?? ""
        }

        return inputCell
    case 2:... // Continues for all the other cells
like image 804
Loic Verrall Avatar asked Nov 08 '22 20:11

Loic Verrall


1 Answers

Figured it out. So the line of code that was causing the problem was actually in viewDidAppear and it was:

inputTextView?.becomeFirstResponder()

I have no idea why it caused the cell not to appear, but removing that line fixed the issue.

like image 200
Loic Verrall Avatar answered Nov 15 '22 10:11

Loic Verrall