Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从UICollection中删除项目,但获得一个“";数组索引超出范围“;错误_Ios_Swift_Parse Platform_Uicollectionview - Fatal编程技术网

Ios 从UICollection中删除项目,但获得一个“";数组索引超出范围“;错误

Ios 从UICollection中删除项目,但获得一个“";数组索引超出范围“;错误,ios,swift,parse-platform,uicollectionview,Ios,Swift,Parse Platform,Uicollectionview,我使用Parse作为后端,并将项目加载到UICollectionView中。一切都很好,但我似乎无法正确删除某些范围的项目。 以下是我的删除功能: func deleteButtonTapped() { let indexPaths = collectionView!.indexPathsForSelectedItems()! self.collectionView!.performBatchUpdates({ for index in indexPaths { s

我使用Parse作为后端,并将项目加载到UICollectionView中。一切都很好,但我似乎无法正确删除某些范围的项目。 以下是我的删除功能:

func deleteButtonTapped() {
  let indexPaths = collectionView!.indexPathsForSelectedItems()!

  self.collectionView!.performBatchUpdates({
    for index in indexPaths {
      self.tasks[index.item].deleteInBackground()
      self.tasks.removeAtIndex(index.item)
    }
  }, completion: nil)

  self.collectionView!.deleteItemsAtIndexPaths(indexPaths)
}
我得到错误:致命错误:数组索引超出范围 我认为这是因为当for循环循环时,它丢失了一个项,但保持了原始计数。我检查以确保self.tasks和indexPaths返回我期望的结果。不知道还有什么可以尝试的。
我如何重构这个?任何帮助或指导都将不胜感激。谢谢

尝试放置
self.collectionView!。deleteItemSatinExpaths(indexPaths)
performBatchUpdates
的完成处理程序中

编辑


在访问self.tasks时,您似乎正在尝试将其从self.tasks中删除。例如,假设您的任务有5个项目[A、B、C、D、E],您选择删除D&E。在第一次迭代中,您删除索引3(即D)处的项目。现在任务有4项[A、B、C、E]。在下一次迭代中,当您尝试访问索引4处的项时(上一次删除之前指向E),您将得到索引越界错误。

尝试放置
self.collectionView!。deleteItemSatinExpaths(indexPaths)
performBatchUpdates
的完成处理程序中

编辑


在访问self.tasks时,您似乎正在尝试将其从self.tasks中删除。例如,假设您的任务有5个项目[A、B、C、D、E],您选择删除D&E。在第一次迭代中,您删除索引3(即D)处的项目。现在任务有4项[A、B、C、E]。在下一次迭代中,当您尝试访问索引4处的项时(上一次删除之前指向E),您将得到索引越界错误。

不幸的是,我得到了相同的错误。这个错误实际上发生在for循环的内部,所以它从来没有走那么远。不幸的是,我得到了同样的错误。这个错误实际上发生在for循环内部,所以它不会走那么远。