从UICollectionView中删除项目 [英] Remove an item from a UICollectionView
问题描述
我有一组图像显示在 UICollectionView
中。当用户点击图像时,它会生成一个 UIActionSheet
,其中包含该图像的一些选项。其中一个人从 UICollectionView
中删除照片。当用户在 UIActionSheet
中选择删除按钮时,会弹出一个警告视图,要求确认。如果用户选择是,则应删除照片。
I have a set of images showing in a UICollectionView
. When the user taps on an image, it spawns a UIActionSheet
with a few options for that image. One of them id removing the photo from the UICollectionView
. When the user selects remove button in the UIActionSheet
, it pops up an alert view asking for confirmation. If the user selects yes, it should remove the photo.
我的问题是,要从 UICollectionView
,您必须将 indexPath
传递给 deleteItemsAtIndexPaths
事件。由于在警报视图的 didDismissWithButtonIndex
事件中授予最终确认,我无法找到获取 indexPath
从那里选择的图像将其传递给 deleteItemsAtIndexPaths
事件。我该怎么做?
My problem is, to remove the item from the UICollectionView
, you have to pass the indexPath
to the deleteItemsAtIndexPaths
event. Since the final confirmation is granted in the alert view's didDismissWithButtonIndex
event, I can't figure out a way to get the indexPath
of the selected image from there to pass it to deleteItemsAtIndexPaths
event. How can I do this?
这是我的代码:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
deletePhotoConfirmAlert = [[UIAlertView alloc] initWithTitle:@"Remove Photo"
message:@"Do you want to remove this photo?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil, nil];
[deletePhotoConfirmAlert addButtonWithTitle:@"Yes"];
[deletePhotoConfirmAlert show];
break;
case 1:
NSLog(@"To Edit photo");
break;
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView == deletePhotoConfirmAlert) {
if (buttonIndex == 1) {
// Permission to delete the button is granted here.
// From here deleteItemsAtIndexPaths event should be called with the indexPath
}
}
}
- (void)deleteItemsAtIndexPaths:(NSArray *)indexPaths
{
}
推荐答案
为什么不使用 [self.collectionView indexPathsForSelectedItems]; 。我这样做是为了一次删除多个图像。
Why not make use of [self.collectionView indexPathsForSelectedItems]; . I have done this for deleting multiple images at a time.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (alertView == deletePhotoConfirmAlert) {
if (buttonIndex == 1) {
// Permission to delete the button is granted here.
NSArray *selectedItemsIndexPaths = [self.collectionView indexPathsForSelectedItems];
// Delete the items from the data source.
[self deleteItemsFromDataSourceAtIndexPaths:selectedItemsIndexPaths];
// Now delete the items from the collection view.
[self.collectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths];
}
}
}
// This method is for deleting the selected images from the data source array
-(void)deleteItemsFromDataSourceAtIndexPaths:(NSArray *)itemPaths {
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for (NSIndexPath *itemPath in itemPaths) {
[indexSet addIndex:itemPath.row];
}
[self.images removeObjectsAtIndexes:indexSet]; // self.images is my data source
}
编辑
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSArray *indexpaths = [self.collectionView indexPathsForSelectedItems];
DetailViewController *dest = [segue destinationViewController];
dest.imageName = [self.images objectAtIndex:[[indexpaths objectAtIndex:0] row]];
}
这篇关于从UICollectionView中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!