Isu*_*uru 4 uialertview uiactionsheet ios uicollectionview
我有一组图像显示在UICollectionView.当用户点击图像时,它会UIActionSheet为该图像生成一些选项.其中一人正在删除照片UICollectionView.当用户选择中的删除按钮时UIActionSheet,会弹出一个要求确认的警报视图.如果用户选择是,则应删除照片.
我的问题是,要从中删除项目UICollectionView,您必须将其传递indexPath给deleteItemsAtIndexPaths事件.由于在警报视图的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
{
}
Run Code Online (Sandbox Code Playgroud)
为什么不使用[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
}
Run Code Online (Sandbox Code Playgroud)
编辑
-(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]];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12927 次 |
| 最近记录: |