gpt4 book ai didi

ios - 从 UICollectionView 中删除一个项目

转载 作者:可可西里 更新时间:2023-11-01 05:09:27 24 4
gpt4 key购买 nike

我在 UICollectionView 中显示了一组图像。当用户点击图像时,它会生成一个 UIActionSheet,其中包含该图像的一些选项。其中之一是从 UICollectionView 中删除照片。当用户选择 UIActionSheet 中的删除按钮时,它会弹出一个警告 View ,要求确认。如果用户选择是,它应该删除照片。

我的问题是,要从 UICollectionView 中删除项目,您必须将 indexPath 传递给 deleteItemsAtIndexPaths 事件。由于最终确认是在警报 View 的 didDismissWithButtonIndex 事件中授予的,因此我想不出一种方法来从那里获取所选图像的 indexPath 并将其传递给 deleteItemsAtIndexPaths 事件。我该怎么做?

这是我的代码:

- (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]; 。我这样做是为了一次删除多张图片。

- (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]];
}

关于ios - 从 UICollectionView 中删除一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16080322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com