我在 Xcode 4.6 中有一个带有 Collection View 的应用程序。我在每个 Collection View 单元格中添加了一个删除按钮,以便为用户提供删除单元格的选项。
虽然我意识到 Collection View 类似于 TableView (并且我知道如何从 TableView 中删除单元格),但删除对象在 Collection View 中的工作方式似乎有所不同。我查看了 Collection View 的类引用,但作为 iOS 的新手,我无法将他们建议的代码应用到我的项目中。该文档显示了以下用于删除单元格的建议代码:
[self.collectionView performBatchUpdates:^{
NSArray* itemPaths = [self.collectionView indexPathsForSelectedItems];
// Delete the items from the data source.
[self deleteItemsFromDataSourceAtIndexPaths:itemPaths];
// Now delete the items from the collection view.
[self.collectionView deleteItemsAtIndexPaths:tempArray];
} completion:nil];
所以我在我的按钮操作方法中添加了这段代码。在它说“临时数组”的地方,我替换了 Collection View 用作其数据源的数组,以确定要显示的单元格数。但我对“deleteItemsFromDataSourceAtIndexPaths:”是什么感到困惑。这是委托(delegate)方法吗?编译器警告它未声明。我不确定从这里去哪里。
感谢任何帮助。
最佳答案
And where it says "temp array", I substituted the array that the collection view is using as its data source to determine the number of cells to display.
这是您的第一个问题:数组很可能不包含 NSIndexPath 的实例,而这正是该方法所期望的。您可能想要传入 itemPaths
数组,这将使 Collection View 从字面上删除“所有选定行的索引路径”。
"deleteItemsFromDataSourceAtIndexPaths:" is. Is this a delegate method?
不,那是伪代码。 Apple 认为没有必要讨论如何在循环中从数组中删除对象,我也没有。您必须自己实现该方法,或者希望 NSIndexPath 不会起作用,并使用 -removeObjectsInArray:
以正确更新您的数据源。
关于ios - 从 Collection View Xcode 中删除 Collection View Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15816182/