ios - 我可以控制 UICollectionView (Swift) 中有多少个单元格保留在内存中吗?

标签 ios swift uicollectionview

是否可以在内存中保留更多的单元格?我在滚动时遇到问题。例如,我能否在内存中保留 3 个屏幕的单元格而不是一个?

如果是这样,怎么能做到这一点?

下面是我的一些单元格的屏幕截图。它只是3个标签。他们是 self 调整的。也许这就是需要这么长时间的原因。

enter image description here

或者很可能是我在 collectionView:cellForItemAtIndexPath:

中做错了什么

这是代码:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let wordCell: ReadArticleCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("wordCell", forIndexPath: indexPath) as ReadArticleCollectionViewCell

        wordCell.pronunciationLabelView.hidden = false
        wordCell.pronunciationLabelView.textColor = UIColor.blackColor()
        wordCell.layer.shadowOpacity = 0
        wordCell.layer.shadowRadius = 0
        wordCell.layer.shadowColor = UIColor.clearColor().CGColor
        wordCell.underlineLabelView.backgroundColor = UIColor.blackColor()

        var arrayOfParagraphsOfWordStrings = [[String]]()

        for paragraph in self.arrayOfParagraphsOfSentencesOfWordStrings { 
            arrayOfParagraphsOfWordStrings.append(paragraph.reduce([], +)) //is this my culprit? I am doing this so I can use a 3d array as a 2d array datasource but still later be able to map from the 2d array's index to the corresponding index in the 3d array.
        }

        if let word = arrayOfParagraphsOfWordStrings[indexPath.section][indexPath.row] as String? {

            if let pinyinArrayForWord = self.pinyinArray[indexPath.section][indexPath.row] as String? {

                if let pinyin = convertPinyinNumbersToToneMarks(self.pinyinArray[indexPath.section][indexPath.row]) as String? {
                    wordCell.pronunciationLabelView.text = pinyin
                }
                else {
                    wordCell.pronunciationLabelView.text = self.pinyinArray[indexPath.section][indexPath.row]
                }

                if wordCell.pronunciationLabelView.text == "" {
                    wordCell.pronunciationLabelView.text = "n/a"
                    wordCell.pronunciationLabelView.hidden = true
                }

                if self.pinyinQuantityArray[indexPath.section][indexPath.row] > 1 {
//                  println(pinyinQuantityArray[indexPath.section][indexPath.row])
                    wordCell.pronunciationLabelView.textColor = UIColor.purpleColor()
                }
            }

            if word == "Score Paragraph" {
                wordCell.wordLabelView.hidden = false
                wordCell.pronunciationLabelView.hidden = true
                wordCell.pronunciationLabelView.textColor = UIColor.redColor()
            }

            switch self.wordScoreArray[indexPath.section][indexPath.row] {
            case 5...10:
                wordCell.pronunciationLabelView.hidden = true
            case 1...10:
                wordCell.underlineLabelView.backgroundColor = UIColor.blueColor()
            case (-10)...(-1):
                wordCell.underlineLabelView.backgroundColor = UIColor.greenColor()
            default:
                wordCell.underlineLabelView.backgroundColor = wordCell.underlineLabelView.backgroundColor
            }

            if self.wordTouchedArray[indexPath.section][indexPath.row] == true {
//              wordCell.underlineLabelView.backgroundColor = UIColor.orangeColor()
//              wordCell.layer.shadowOffset = CGSize(width: 10, height: 20)
                wordCell.layer.shadowOpacity = 0.75
                wordCell.layer.shadowRadius = 6
                wordCell.layer.shadowColor = UIColor.yellowColor().CGColor
//              wordCell.underlineLabelView.layer.borderColor = UIColor.blackColor().CGColor
//              wordCell.underlineLabelView.layer.borderWidth = 0.25
//              wordCell.underlineLabelView.layer.shadowColor = UIColor.blackColor().CGColor
//              wordCell.underlineLabelView.layer.shadowOffset = CGSize(width: 1, height: 1)
            }

            if self.wordLookedUpArray[indexPath.section][indexPath.row] == true {
//              wordCell.underlineLabelView.backgroundColor = UIColor.blackColor()
                wordCell.layer.shadowOpacity = 0.75
                wordCell.layer.shadowRadius = 6
                wordCell.layer.shadowColor = UIColor.yellowColor().CGColor
            }

            wordCell.wordLabelView.text = arrayOfParagraphsOfWordStrings[indexPath.section][indexPath.row]
        }

        return wordCell
    }

最佳答案

您应该将此代码移动到 ViewDidload 或 ViewWillAppear(在加载 Collection View 之前)。每次绘制单元格时,您都会重新调用它。我想这是不必要的。

var arrayOfParagraphsOfWordStrings = [[String]]()

for paragraph in self.arrayOfParagraphsOfSentencesOfWordStrings { 
    arrayOfParagraphsOfWordStrings.append(paragraph.reduce([], +)) //is this my culprit? I am doing this so I can use a 3d array as a 2d array datasource but still later be able to map from the 2d array's index to the corresponding index in the 3d array.
}

关于ios - 我可以控制 UICollectionView (Swift) 中有多少个单元格保留在内存中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28833246/

相关文章:

ios - 不要删除我 map 上的所有注释

ios - 扩展函数的返回值?

swift - UIView Nib 中的 UICollectionView

ios - 检查连接并执行到第二个 View Controller 的 segue

ios - Swift: strikethroughStyle For Label (Middle Delete Line For Label)

iphone - 应用程序未针对模拟器进行编译

ios - uicollectionview象棋风格网格

ios - 有什么方法可以在 swift 4 中迭代 double var 的小数部分吗?

ios - 如何在循环内对 UIView 进行动画 n 次并跟踪每个动画事件

ios - 为什么我的 UICollectionView 单元格在滚动后消失了?