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

时间:2016-06-22 09:13:50

标签: ios objective-c xcode swift uicollectionview

对于单元格我只是从数组中输入信息:

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 cell - PictureA

2 cell - PictureB

然后滚动,并在collectionview中看到:

1 cell - PictureB

2 cell - PictureA

然后滚动,并在collectionview中再次看到:

1 cell - PictureA

2 cell - PictureB

无法理解这里发生的事情......

3 个答案:

答案 0 :(得分:0)

原因是表格视图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
    }

答案 1 :(得分:0)

取消支票。因为细胞被重复使用。您需要每次都设置数据

 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

答案 2 :(得分:0)

override func prepareForReuse() {
    super.prepareForReuse()

      cell.dateLabel.text = nil
        cell.image.image = nil

}