是否可以在内存中保留更多的单元格?我在滚动时遇到问题。例如,我能否在内存中保留 3 个屏幕的单元格而不是一个?
如果是这样,怎么能做到这一点?
下面是我的一些单元格的屏幕截图。它只是3个标签。他们是 self 调整的。也许这就是需要这么长时间的原因。
或者很可能是我在 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/