繁体   English   中英

Swift 2,UiCollectionview滚动后-更改了单元格数据

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM