![](/img/trans.png)
[英]Swift UICollectionView Cell favourite button after scrolling changed to unfavorite
[英]Swift 2, UiCollectionview after scrolling - changed cells data
對於單元格,我只是從數組中放入信息:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! ImagesCollectionViewCell
if cell.dateLabel.text == "" {
cell.dateLabel.text = imagesDatas[indexPath.row].datesStr
cell.image.image = imagesDatas[indexPath.row].images
}
return cell
}
要進行陣列,我在DidAppear中生成數據。 只有一次。 但是,當我滾動collectionView時,單元格會更改數據,例如:
1格-PictureA
2格-PictureB
然后滾動,在collectionview中將看到:
1個單元格-PictureB
2格-PictureA
然后滾動,然后在collectionview中將再次看到:
1格-PictureA
2格-PictureB
不明白這里發生了什么...
原因是表視圖dequeue
你的優化,所以你最后一個單元出隊。 您應該像這樣完成代碼:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! ImagesCollectionViewCell
if cell.dateLabel.text == "" {
cell.dateLabel.text = imagesDatas[indexPath.row].datesStr
cell.image.image = imagesDatas[indexPath.row].images
}
else {
cell.dateLabel.text = "" ( (or another string)
cell.image.image = nil(or another image)
}
return cell
}
刪除支票。 由於單元被重用。 您需要每次設置數據
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! ImagesCollectionViewCell
cell.dateLabel.text = imagesDatas[indexPath.row].datesStr
cell.image.image = imagesDatas[indexPath.row].images
return cell
override func prepareForReuse() {
super.prepareForReuse()
cell.dateLabel.text = nil
cell.image.image = nil
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.