如何通过长按该单元格来删除UICollectionView中的单元格,并且必须显示删除按钮才能删除这些选定的单元格?

时间:2018-06-08 08:51:47

标签: ios objective-c uicollectionview uicollectionviewcell

如何通过长按该单元格来删除UICollectionView中的单元格,并且必须显示删除按钮才能删除这些选定的单元格?

嘿伙计我正在创建一个使用相机和图库来挑选图像的应用程序,这些图像保存在coredata中并在UIColloctionView中显示。

这是我的问题。
我想添加一个删除选项,必须通过长按所选单元格来弹出?

我在这里分享我的代码。

请帮助我。

#import "Mynewviewcontroller.h"
#import "ImageCollectionViewCell.h"
#import "AppDelegate.h"
#import "pushimageViewController.h"

    @interface Mynewviewcontroller ()
    <UICollectionViewDataSource,UICollectionViewDelegate,UIGestureRecognizerDelegate>
    {
        AppDelegate *appdelegate;
        NSManagedObjectContext *context;
        NSMutableArray *_mutableArray;
    }
    @end

    @implementation Mynewviewcontroller
    @synthesize mycollectionview;
    - (void)viewDidLoad {
        [super viewDidLoad];
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

            UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                  message:@"Device has no camera"
                                                                 delegate:nil
                                                        cancelButtonTitle:@"OK"
                                                        otherButtonTitles: nil];

            [myAlertView show];


          self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"  style:UIBarButtonItemStylePlain target:self action:@selector(cancelAction)];



        }

        _mutableArray = [[NSMutableArray alloc] init];

        [self fetch];


        [self.mycollectionview reloadData];






    }






    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return _mutableArray.count;
    }

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        ImageCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

        //    UIImageView *dkimg = (UIImageView *)[cell viewWithTag:100];
        //
        //    dkimg.image = [UIImage imageNamed:[_mutableArray objectAtIndex:indexPath.row]];

        cell.dkimgview.image = (UIImage*) [_mutableArray objectAtIndex:indexPath.row];

        return cell;
    }


    -(void)collectionView:(UICollectionView *)collectionView
    didSelectItemAtIndexPath:(NSIndexPath *)indexPath {


    //anotherviewcontroller

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        pushimageViewController *homeVC = [storyboard instantiateViewControllerWithIdentifier:@"push"];

        homeVC.imgstr =[_mutableArray objectAtIndex:indexPath.row];


        [self.navigationController pushViewController:homeVC animated:YES];






    }




    - (CGFloat)collectionView:(UICollectionView *)collection Viewlayout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
    {
        return 0;
    }


    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }


    - (IBAction)useCamera:(id)sender {

        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentViewController:picker animated:YES completion:NULL];

    }



    - (IBAction)useCameraRoll:(id)sender {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentViewController:picker animated:YES completion:NULL];
    }


    -(void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
        NSData *imageData = UIImagePNGRepresentation(chosenImage);

        appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
        context = appdelegate.persistentContainer.viewContext;


        //save coredata
        NSManagedObject *entity = [NSEntityDescription insertNewObjectForEntityForName:@"Images" inManagedObjectContext:context];

        [entity setValue:imageData forKey:@"firstimage"];

        [appdelegate saveContext];

         [_mutableArray removeAllObjects];

        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Can't Save");
        }else{
            NSLog(@"hifi");
        }

        NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Images"];
        NSError *errr = nil;

        NSArray *array = [context executeFetchRequest:fetchRequest error:&errr];

        if (array == nil){
            NSLog(@"there is no data");
        }else{

            if (array.count > 0) {
                for (int a = 0; a < [array count]; a++)
                {
                    NSData * dataBytes = [[array objectAtIndex:a] valueForKey:@"firstimage"];
                    UIImage *imae = [UIImage imageWithData:dataBytes];

                    [_mutableArray addObject:imae];


                }

            }
        }




        [self.mycollectionview reloadData];


        [picker dismissViewControllerAnimated:YES completion:NULL];
    }

    - (void)fetch; {

        //fetch

        appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
        context = appdelegate.persistentContainer.viewContext;
        NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Images"];
        NSError *errr = nil;

        NSArray *array = [context executeFetchRequest:fetchRequest error:&errr];

        if (array == nil){
            NSLog(@"there is no data");
        }else{

            if (array.count > 0) {
                for (int a = 0; a < [array count]; a++)
                {
                    NSData * dataBytes = [[array objectAtIndex:a] valueForKey:@"firstimage"];
                    UIImage *imae = [UIImage imageWithData:dataBytes];

                    [_mutableArray addObject:imae];


                }

            }
        }
    }
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

        [picker dismissViewControllerAnimated:YES completion:NULL];

    }

0 个答案:

没有答案