从集合视图 Xcode 中删除集合视图单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15816182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete Collection View Cell from Collection View Xcode
提问by jac300
I have an application in Xcode 4.6 with a collection view. I have added a delete button to each collection view cell to offer the user the option to delete the cell.
我在 Xcode 4.6 中有一个带有集合视图的应用程序。我在每个集合视图单元格中添加了一个删除按钮,为用户提供删除单元格的选项。
Although I realize that a collection view is similar to a table view (and I know how to delete cells from a table view), deleting objects seems to work differently in a collection view. I looked in the class reference for collection views, but being new to iOS, I am having trouble applying their suggested code to my project. The documentation shows the following suggested code for deleting cells:
尽管我意识到集合视图类似于表视图(并且我知道如何从表视图中删除单元格),但在集合视图中删除对象的工作方式似乎有所不同。我查看了集合视图的类参考,但作为 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];
So I added this code inside my button action method. 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. But I am confused as to what "deleteItemsFromDataSourceAtIndexPaths:" is. Is this a delegate method? The compiler warns that it is not declared. I'm not sure where to go from here.
所以我在我的按钮操作方法中添加了这段代码。在它说“临时数组”的地方,我替换了集合视图用作其数据源的数组,以确定要显示的单元格数量。但我对“deleteItemsFromDataSourceAtIndexPaths:”是什么感到困惑。这是一个委托方法吗?编译器警告它未声明。我不知道从哪里开始。
Any help is appreciated.
任何帮助表示赞赏。
回答by CodaFi
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.
在它说“临时数组”的地方,我替换了集合视图用作其数据源的数组,以确定要显示的单元格数量。
That's your first problem: the array, more than likely, does not contain instances of NSIndexPath, which is what that method expects. What you probably want is to pass in the itemPaths
array, which would make the collection view remove literally the "index paths of all selected rows."
这是您的第一个问题:该数组很可能不包含 NSIndexPath 的实例,这正是该方法所期望的。您可能想要的是传入itemPaths
数组,这将使集合视图从字面上删除“所有选定行的索引路径”。
"deleteItemsFromDataSourceAtIndexPaths:" is. Is this a delegate method?
“deleteItemsFromDataSourceAtIndexPaths:”是。这是一个委托方法吗?
Nope, that's pseudo-code. Apple didn't see it necessary to digress into a discussion on how to remove objects from an array in a loop, and neither do I. You have to implement that method yourself, or hope that NSIndexPath doesn't act up, and use -removeObjectsInArray:
to update your data source properly.
不,那是伪代码。苹果并没有看到有必要离题到如何在一个循环数组删除对象的讨论,和我也不会。你必须实现该方法自己,或希望NSIndexPath不行动起来,和使用-removeObjectsInArray:
到正确更新您的数据源。
回答by user1686758
Clear sampleis available here. Delete from the datasource first and then delete those indexPaths from the view. 2 step operation and should be done in the same sequence.
此处提供透明样品。首先从数据源中删除,然后从视图中删除那些 indexPath。2 步操作,应按相同顺序进行。