2016-04-10 108 views
0

我正在創建相機應用程序。我在收藏視圖中顯示拍攝的照片。我放置了一個按鈕來刪除特定的圖片。在運行時,我可以看到要刪除的按鈕,但如果我點擊該按鈕,它不會執行任何操作。按鈕點擊方法在集合視圖中不起作用

-(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 
} 

任何人都可以幫助我嗎?

+1

爲什麼要添加刪除到UIImageView的按鈕?將它添加到單元格。也嘗試添加按鈕像UIButton * deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];或UIButton * deleteButton = [UIButton buttonWithType:UIButtonTypeCustom]; –

+0

感謝Arun Gupta,它的工作,我只是將我的按鈕添加到我的單元格 – user6183984

回答

-1

我想你可能會丟失

[collectionView reloadData] 


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

} 
+0

謝謝伊曼,我知道我想重新加載collectionview.if我把一個斷點刪除方法它不會在該方法下.i我不知道爲什麼 – user6183984

+0

啊好的,該方法應該是這樣的(IBAction)刪除:(UIButton *)發件人而不是void – Iman

+0

Iman,如果我使用IBAction的手段,該按鈕應該創建在故事板右側,我創建按鈕編程只 – user6183984