【发布时间】:2017-06-19 18:48:27
【问题描述】:
我正在使用带有自定义布局的 UICollectionView,但我遇到了向下滚动和备份后单元格消失的问题。
请查看我的问题的video demonstration。
我在谷歌上搜索了一些,其他人也遇到了相关问题,我认识到这可能是由于细胞重复使用造成的,但我在其他地方找到的答案都没有帮助我。
所以我的问题是:
为什么会这样?
如何阻止这种情况发生?
有趣的是,一旦我提出 UIViewController 并关闭它,问题就不再出现了。请查看video 的实际操作。
注意:这个错误已经存在了一段时间(至少从 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
【问题讨论】:
-
1. iOS 删除了单元格以节省资源,因为它没有显示并且您没有正确地重新初始化它。 2. 正确重新初始化单元。 (你认为我们是魔术师,真的可以在没有任何相关代码的情况下帮助你吗?)
-
感谢您的回复和建议发布一些相关代码。我将编辑我的问题以包含 cellForItem 方法的适当部分。如果您还有其他需要,请告诉我。
-
什么是inputTextView?它存储在哪里?
-
inputTextView 是对存储在
UIViewController中的UITextView的引用,其中包含UICollectionView。
标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout