【发布时间】:2015-03-03 13:38:20
【问题描述】:
是否可以在内存中保留更多单元格?我在滚动时遇到问题。例如,我可以在内存中保留 3 个屏幕而不是一个单元吗?
如果是这样,怎么能做到这一点?
下面是我的一些单元格的屏幕截图。它只有 3 个标签。它们是自定尺寸的。也许这就是需要这么长时间的原因。
或者很可能是我在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
}
【问题讨论】:
-
你应该修改你的代码以避免UI线程中的所有长时间运行的进程。即使您可以增加内存中的屏幕数量,您仍然无法加载将在滚动时显示在屏幕中的第 4 个屏幕。
-
我确实想避免在 UI 线程中长时间运行进程,但是我是一个编程新手,我只是想把它搞定。现在我只能在主要滞后开始之前滚动不到一英寸。至少如果我在内存中保留更多单元格,我可以进一步滚动,然后阅读一点,然后更多单元格将在后台加载(对吗?),然后我可以再次滚动并阅读更多内容。
-
“这是我的罪魁祸首吗?”是的,就是这个!
标签: ios swift uicollectionview