0

I am creating a camera app. I am showing captured pictures in collection view. I placed a button to delete the particular picture. While running I am able to see the button to delete, but if I click the button it is not performing any action.

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

    Cell.self.image_View.image=[self.imageArray objectAtIndex:indexPath.row];

    UIButton *deleteButton = [[UIButton alloc]init];
    deleteButton.frame = CGRectMake(80, 0, 20, 20);
    //deleteButton.backgroundColor = [UIColor redColor];
    [deleteButton setImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
    [deleteButton setTag:indexPath.row];

    [deleteButton addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];

    [Cell.self.image_View addSubview:deleteButton];

    return Cell;
 }

 -(void) delete:(UIButton*)sender{
      UIButton *btn = (UIButton *)sender;
      [self.imageArray removeObjectAtIndex:btn.tag];
      //reload your collectionview here
 }

Can anyone help me?

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
  • 1
    Why you are adding delete button on to the UIImageView? Add it to the cell. Also try to add the button like UIButton *deleteButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; or UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom]; – Arun Gupta Apr 10 '16 at 14:11
  • thanks Arun Gupta , Its worked , i just add my button to my cell – user6183984 Apr 10 '16 at 14:28

1 Answers1

-1

i think you might be missing

[collectionView reloadData] 


    -(void) delete:(UIButton*)sender{
    UIButton *btn = (UIButton *)sender;
   [self.imageArray removeObjectAtIndex:btn.tag];
    [collectionView reloadData] 

}
Iman
  • 1,097
  • 2
  • 11
  • 19
  • Thanks Iman , i know that i want to reload the collectionview.if i put a break point in delete method it is not going under that method .i dont know why – user6183984 Apr 10 '16 at 13:17
  • ah ok, the method should be like that (IBAction) delete:(UIButton*)sender instead of void – Iman Apr 10 '16 at 13:19
  • Iman, if i use IBAction means,that button should be created in storyboard right , i created the button programmatically only – user6183984 Apr 10 '16 at 13:44