raj
raj

Reputation: 93

How to delete a particular UICollectionViewCell image. On pressing the button on the particular image

Hello every one please help me in this: As I took a UICollectionView in this UICollectionView I am adding the images, I want to delete a particular Image on button click. the image I want to delete is having the close button on it. How could I perform this task. Note: 1)Please see the image once before helping. 2)Please see the code I did for adding Image.

Image: My Image

//**** For image work ****
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ident = @"Cell";
    OwnerPropertyCollectionViewCell *cell = (OwnerPropertyCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:ident forIndexPath:indexPath];
    imgView = (UIImageView*)[cell viewWithTag:100];

    if (indexPath.row ==0) {
        cell.Imgprofile_pic.image = [UIImage imageNamed:[imgArray objectAtIndex:indexPath.row]];
        cell.btnImageCancel.image = [UIImage imageNamed:@"add"];

        UITapGestureRecognizer *reconizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addphoto:)];
        [cell addGestureRecognizer:reconizer];

    }
    else {
        // get image not name
        cell.Imgprofile_pic.image = [imgArray objectAtIndex:indexPath.row];
        cell.btnImageCancel.image = [UIImage imageNamed:@"close"];

    }

    return  cell;
}
// To add image:
-(void)addphoto:(UITapGestureRecognizer*)reconizer
{
    imageHelper = [UIImagePickerHelper new];
    [imageHelper imagePickerInViewController:self WithSuccess:^(UIImage *image) {
        imgView.image = image;
        [imgArray addObject:image];

        [self.collectionView reloadData];
    } failure:^(NSError *error) {

    }];
    NSLog(@"Image added successfully");


}

// What to code for delete image:
- (IBAction)btnCancelButtonAction:(id)sender {


    NSLog(@"Delete button pressed");


}

Upvotes: 0

Views: 832

Answers (2)

Gurinder Batth
Gurinder Batth

Reputation: 663

First of all add the following lines

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.yourCollectionView];  
NSIndexPath *indexPath = [self.yourCollectionView indexPathForItemAtPoint:buttonPosition];

in this way you get the indexpath of the item and then

[YourImageArray removeObjectAtIndex:indexPath];
[self.yourCollectionView reloadData];

If need any help ask me

Upvotes: 0

Niharika
Niharika

Reputation: 1198

Take a tag value as a cell index path for your selected delete button.(ex : for first row button tag will be 0) then remove that image from your array. Then reload your UICollectionView

[imgArray removeObjectAtIndex:index];
[self.collectionView reloadData];

Upvotes: 1

Related Questions