2017-01-16 37 views
0

你好每一個人請幫助我在這: 因爲我在這UICollectionViewUICollectionView我加入圖像,我想刪除按鈕點擊一個特定的圖像。 我想要刪除的圖像上有關閉按鈕。 我如何執行此任務。 注: 1)請在幫助之前查看圖像一次。 2)請參閱添加圖像的代碼。如何刪除特定的UICollectionViewCell圖像。在按下特定圖像上的按鈕

圖片: 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"); 


} 

回答

1

以一個標籤值作爲選擇刪除按鈕,開始小區索引路徑(例如:對於第一行按鈕標籤將爲0)。 然後從數組中刪除該圖像。然後重新裝入您的UICollectionView

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

謝謝你,但我仍然有點困惑。但我正在努力,謝謝。 – raj

0

首先,添加以下行

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.yourCollectionView]; 
NSIndexPath *indexPath = [self.yourCollectionView indexPathForItemAtPoint:buttonPosition]; 
這樣你獲得該項目的indexpath

,然後

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

如果需要任何幫助,問我

+0

嘿謝謝,我是否必須在按鈕的動作中添加此代碼?像這樣: - (IBAction)btnCancelButtonAction:(id)sender CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.collectionView]; NSIndexPath * indexPath = [self.collectionView indexPathForItemAtPoint:buttonPosition]; [imgArray removeObjectAtIndex:indexPath]; [self.collectionView reloadData]; NSLog(@「刪除按鈕按下」); } – raj

+0

是粘貼這個編碼的按鈕操作,然後它有任何問題,然後問我 –

+0

如果這不起作用,然後將標籤給按鈕。像這樣一個按鈕單元類的出口鏈接按鈕與該出口和'cellForItemAtIndexPath'方法給這個標籤,像這樣'cell.yourButton.tag = indexPath.item' –

相關問題