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

时间:2015-03-03 13:38:21

标签: ios swift uicollectionview

是否可以在内存中保留更多的细胞?我在滚动时遇到问题。例如,我可以在内存中保留3个屏幕,而不是只有一个吗?

如果是这样,那怎么可能呢?

下面是我的一些细胞的截图。它只有3个标签。他们是自我调整大小。也许这就是花了这么长时间。

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
    }

2 个答案:

答案 0 :(得分:0)

  

例如,我可以在内存中保留3个屏幕,而不是只有一个吗?

没有必要在内存中保留超过一个屏幕的单元格,因为它们一次只能呈现一个屏幕。 Cells表示MVC结构中的视图,因此您只需创建所需数量的视图。

另一方面,将单元格中的数据保留在内存中非常有意义。数据代表MVC中的模型;这就是缓存应该发生的地方。

  

现在我只能在重大滞后开始前滚动不到一英寸。

这是因为collectionView:cellForItemAtIndexPath:中的代码需要访问尚未缓存在数据源中的数据。要解决此问题,请将准备单元的代码拆分为两部分:一部分用于获取和缓存数据,并准备制作UICollectionViewCell所需的所有内容而不实际制作,另一部分可能需要回收UICollectionViewCell,快速将其与缓存中的数据配对,然后将其发送到UICollectionView

编辑:(响应问题的编辑)

var arrayOfParagraphsOfWordStrings = [[String]]()
for paragraph in self.arrayOfParagraphsOfSentencesOfWordStrings { 
    arrayOfParagraphsOfWordStrings.append(paragraph.reduce([], +))
}
  

这是我的罪魁祸首吗?我这样做,所以我可以使用3d数组作为2d数组数据源,但后来仍然可以从2d数组的索引映射到3d数组中的相应索引。

是的,这就是问题所在。您为每个单元格执行此转换,然后从中单个单词。您应该重构代码以将此特定部分移动到其他位置,将arrayOfParagraphsOfWordStrings存储在实例变量中,并在collectionView:cellForItemAtIndexPath:代码中使用它,而无需为每个单元重建它。

答案 1 :(得分:0)

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

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.
}