我正在創建相機應用程序。我在收藏視圖中顯示拍攝的照片。我放置了一個按鈕來刪除特定的圖片。在運行時,我可以看到要刪除的按鈕,但如果我點擊該按鈕,它不會執行任何操作。按鈕點擊方法在集合視圖中不起作用
-(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
}
任何人都可以幫助我嗎?
爲什麼要添加刪除到UIImageView的按鈕?將它添加到單元格。也嘗試添加按鈕像UIButton * deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];或UIButton * deleteButton = [UIButton buttonWithType:UIButtonTypeCustom]; –
感謝Arun Gupta,它的工作,我只是將我的按鈕添加到我的單元格 – user6183984