【发布时间】: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