2017-01-16 6 views
0

こんにちはすべて私はこの中で助けてください: このUICollectionViewの私はUICollectionViewを取ったので、私は画像を追加しています、私はボタンのクリックで特定の画像を削除したい。 削除したい画像に閉じるボタンがあります。 このタスクはどのように実行できますか。 注: 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

まずリロード追加し、次の行

アイテムのindexpathを取得し、このように
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.yourCollectionView]; 
NSIndexPath *indexPath = [self.yourCollectionView indexPathForItemAtPoint:buttonPosition]; 

と任意の助けを必要とする場合は、

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

は私に尋ねます

+0

ありがとう、ボタンのアクションでこのコードを追加する必要がありますか? - (IBAction)btnCancelButtonAction:(id)送信者{ CGPoint buttonPosition = [送信側変換ポイント:CGPointZero toView:self.collectionView]; NSIndexPath * indexPath = [self.collectionView indexPathForItemAtPoint:buttonPosition]; [imgArray removeObjectAtIndex:indexPath]; [self.collectionView reloadData]; NSLog(@ "削除ボタンが押されました"); } – raj

+0

はい、このコードをButtonアクションに貼り付けてから問題があります。 –

+0

これがうまくいかない場合は、ボタンにタグを付けてください。これはボタンセルクラスのリンクアウトボタンです。コンセントと 'cellForItemAtIndexPath'メソッドで' cell.yourButton.tag = indexPath.item'のようなタグを与えてください –

関連する問題