【发布时间】:2015-08-04 20:31:37
【问题描述】:
我有一个存储在图片数组中的 20 个项目的集合视图。我正在使用此代码删除选定的项目:
self.collectionView.performBatchUpdates({
let selectedItems = self.collectionView.indexPathsForSelectedItems()!
self.picturesObject.deleteItemsAtIndexPaths(selectedItems)
self.collectionView.deleteItemsAtIndexPaths(selectedItems)
}, completion: {_ in})
这是picturesObject的deleteItemsAtIndexPaths的实现:
func deleteItemsAtIndexPaths(indexPaths:[NSIndexPath]){
for indexPath in indexPaths{
pictures.removeAtIndex(indexPath.item)
}
}
我面临的问题如下。每次删除集合视图中的选定项目时,图片数组都会变小。如果我删除第一项,图片数组只有 19 项大。如果我尝试删除集合视图中的第 20 项,则数组索引会超出范围,因为图片数组现在只有 19 个对象大,但我想删除存储在 indexpath.item 中的第 20 个对象。
在集合视图中删除项目的正确方法是什么?
EDIT:/// 集合视图方法
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
collectionView.allowsMultipleSelection = true
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return picturesObject.pictures.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! ShopCell
cell.imageView.image = picturesObject.pictures[indexPath.item]
return cell
}
【问题讨论】:
-
嗨,我认为您删除了数组中的图像,但您的 dataSource 委托方法在内存中保留了 20 个项目。您可以粘贴您的 collectionView 委托方法吗?
-
你去。我编辑了我的问题。我认为它们是数据源方法,没有其他方法。
标签: ios arrays uicollectionview uicollectionviewcell