我有一個UICollectionView
的20個單元格,其中有imageView
。當用戶點擊正確的單元格時,我在該單元格中繪製另一個imageView
(一個正確的圓圈我們稱之爲)。這工作正常。iOS 8 Swift UICollectionView reloadData()導致視圖(圖像)跳轉單元格
但我也有一個標題在UICollectionView
,我問他們需要回答的問題。每次點擊後(等待2秒),問題應該更新,我有一個功能,更新標題文本的標籤,這也很好。
我的問題是: 爲了更新的標題文字,我要叫uicollectionView.reloadData()
,每當我這樣做,我在小區(correctCircle)提請UIImageView
被移動到另一個細胞!
我用盡了一切我能想到的,包括在這個線程解決方案: UICollectionView reloadData not functioning properly in iOS 7
但不能讓它開始工作。任何想法都是值得歡迎的。理想情況下,我只會刷新標題而不是其他單元格,但似乎沒有辦法做到這一點。
編輯:基於評論稍微改變代碼,現在我有兩個圖像在每個單元格中,一個隱藏,然後我顯示它,而不是在點擊後添加imageView
。
下面是一些相關的代碼:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewIconCell
// bigIcon() just returns the animal's icon image
cell.icon.image = animalsArray[indexPath.row].bigIcon()
return cell
}
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// we cannot use a dequeueReusableCellWithReuseIdentifier, we need this
let cell = collectionView.cellForItemAtIndexPath(indexPath) as CollectionViewIconCell
if currentAnimal == self.correctAnimal {
cell.circleImage.image = correctCircleImage
cell.circleImage.hidden = false
correctAnswers++
// nextQuestion() simply loads the text that will go in the Header.label
self.nextQuestion()
self.collectionView!.reloadData()
} else {
self.answerCircle.image = UIImage(named: "wrongCircle")
self.answerCircle.sizeToFit()
cell.addSubview(answerCircle)
self.nextQuestion()
self.collectionView!.reloadData()
}
}
感謝。
PS
讀一些評論,也許我不是一個新問題,正確設置爲標題的文本?我從教程中獲得了所有這些代碼,對於UICollectionView來說很新,所以請原諒我的無知,但是有沒有更好的方法來刷新標題文本?我無法獲得viewForSupplementaryElementOfKind
函數以外的標題。
我應該解釋我有2個不同的imageViews,一個用於正確答案,一個用於錯誤答案,因爲正確答案的循環應該保留,而錯誤答案的答案將在每個問題上被刪除並重新使用。
顯示創建單元格的代碼請 – 2015-04-03 08:39:35