如何删除特定的UICollectionViewCell图像。按下特定图像上的按钮

时间:2017-01-16 10:35:11

标签: ios objective-c uicollectionview

大家好,请帮助我: 当我在UICollectionView中添加UICollectionView我正在添加图片时,我想删除按钮点击时的特定图片。 我要删除的图像上有关闭按钮。 我怎么能执行这个任务。 注意: 1)请在帮助之前看一次图像。 2)请参阅我为添加图片所做的代码。

图像: 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");


}

2 个答案:

答案 0 :(得分:1)

将标记值作为所选删除按钮的单元格索引路径。(例如:对于第一行按钮标记将为0) 然后从您的阵列中删除该图像。然后重新加载您的UICollectionView

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

答案 1 :(得分:0)

首先添加以下行

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

以这种方式获得项目的索引路径,然后

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

如果需要任何帮助,请问我