为什么滚动后消失我的UICollectionViewCells?

Loi*_*all 5 ios uicollectionview uicollectionviewcell uicollectionviewlayout swift

我使用的UICollectionView是自定义布局,但是在向下滚动和向上滚动后,单元格消失了,这是我遇到的问题。

请观看有关我的问题的视频演示

我已经进行了一些Google搜索,其他人也遇到了相关问题,并且我认识到这可能是由于单元重用造成的,但是我在其他地方找不到的答案都没有帮助我。

所以我的问题是:

  1. 为什么会这样呢?

  2. 如何阻止这种情况的发生?

有趣的是,一旦我提出UIViewController并关闭了它,该问题就不再发生。请观看实际操作中的视频

注意:此错误已经存在了一段时间(至少自iOS 10起,并且尚未在iOS 11 beta 1中修复)。

编辑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
Run Code Online (Sandbox Code Playgroud)

Loi*_*all 1

弄清楚了。所以导致问题的代码行实际上viewDidAppear是:

inputTextView?.becomeFirstResponder()
Run Code Online (Sandbox Code Playgroud)

我不知道为什么它导致单元格不出现,但删除该行解决了问题。