【问题标题】:CollectionView reloadData in moveItemAtIndexPath resulting duplicate image sometimesmoveItemAtIndexPath 中的 CollectionView reloadData 有时会导致重复图像
【发布时间】:2017-01-16 10:36:15
【问题描述】:
  • 在我的应用程序中,目前我们需要在 collectionView 中重新排序图像,并且 collectionView 的第一张图像应该反映在 imageView 上,所以这是我的实现

    //MARK: Variable declaration
    
     @IBOutlet var imageViewMain: UIImageView!
     @IBOutlet var collectionViewImages: UICollectionView!
     var listingImages : [UIImage] = [UIImage(named:"demo_advertisement")!, UIImage(named:"camera_black")!, UIImage(named:"twitter-bird-logo")!, UIImage(named:"money-bag")!]
    
     //MARK: DataSource
    
       func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
      {
    
        let addPhotosCell = collectionView.dequeueReusableCellWithReuseIdentifier("AddPhototsCollectionViewCell", forIndexPath: indexPath) as! AddPhototsCollectionViewCell
        addPhotosCell.imageView.image = listingImages[indexPath.item]
    
        //        if listingImages.count > 0 {
        //            imageViewMain.image = listingImages[0]
        //        }
    
        return addPhotosCell
      }
    
      func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath,toIndexPath destinationIndexPath: NSIndexPath) {
    
        // swap values if sorce and destination
        let src = listingImages[sourceIndexPath.row]
        let dest = listingImages[destinationIndexPath.row]
        listingImages[destinationIndexPath.row] = src
        listingImages[sourceIndexPath.row] = dest        
    
        self.collectionViewImages.performBatchUpdates({
    
            self.collectionViewImages.reloadData()
    
            }, completion: nil)
    
    }
    

我想要达到的目标:

  • collevtionView 的第一个元素应该显示在 imageViewMain 上
  • 对 collectionView 图像重新排序(我在 ColletionView 上使用长按手势完成了此操作)

问题:

如果我这样做了

   self.collectionViewImages.reloadData()

在重新排序图像时在 moveItemAtIndexPath 中显示重复的图像,例如

我调用 reloadData() 是因为我需要在 cellForItemAtIndexPath 中将 collectionView 的第一个元素设置为 imageViewMain。

我的尝试:如果我不调用 reloadData 并尝试在 moveItemAtIndexPath 中将图像设置为 imageViewMain

    imageViewMain.image = listingImages[0]  

在下面几行之后,它不会正确反映在 imageViewMain 上。(但在这种情况下,图像的重新排序可以完美地工作,没有任何重复的图像)

    let src = listingImages[sourceIndexPath.row]
    let dest = listingImages[destinationIndexPath.row]
    listingImages[destinationIndexPath.row] = src
    listingImages[sourceIndexPath.row] = dest    

谁能建议我实现这个的正确方法是什么?

【问题讨论】:

  • 你试过reloadItemsAtIndexPaths 吗?而不是reloadData()
  • 是的,我也尝试过 reloadSections 但结果相同

标签: ios swift uicollectionview ios9


【解决方案1】:

添加

 addPhotosCell.imageView.image = nil

cellForItemAtIndexPath 的下方dequeueReusableCellWithReuseIdentifier 我的意思是它应该是cellForItemAtIndexPath 的第二行。

现在,为什么会这样?答案是收集视图dequeue 单元和resue 以提高内存效率和更好的性能。所以有时候你没有得到新的图像然后它会显示以前的图像,因为图像没有被覆盖!!

因此,您必须将单元格的 imagview 设置为nil,或者您必须设置一些placeholder image,如果您没有图像,则可以显示!

【讨论】:

  • 好的,让我试试。我会告诉你的。
  • 不!我按照你的建议做了,在将第三个元素重新排序为第二个元素并滚动 collectionView 后,它向我展示了重复的图像。即使我尝试覆盖 func prepareForReuse() { imageView.image = nil }
  • 如果您正在重新排序单元格,那么您必须管理一个包含重新排序顺序的图像的数组。并且您需要在每次重新加载时根据该数组管理cellForItemAtIndexpath
  • addPhotosCell.imageView.image = nil 不起作用,moveItemAtIndexPath 中的逻辑错误。无论如何感谢您的回答。
【解决方案2】:

其实是我写的逻辑

moveItemAtIndexPath

错了。而不是

let src = listingImages[sourceIndexPath.row]
let dest = listingImages[destinationIndexPath.row]
listingImages[destinationIndexPath.row] = src
listingImages[sourceIndexPath.row] = dest  

应该是

let src = listingImages[sourceIndexPath.row]
listingImages.removeAtIndex(sourceIndexPath.row)
listingImages.insert(src, atIndex: destinationIndexPath.row)
collectionViewImages.reloadData()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多