【发布时间】:2016-04-10 12:15:59
【问题描述】:
我正在创建一个相机应用程序。我在收藏视图中显示捕获的图片。我放置了一个按钮来删除特定的图片。运行时,我可以看到要删除的按钮,但如果我单击该按钮,它不会执行任何操作。
-(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,它成功了,我只需将我的按钮添加到我的单元格中
标签: objective-c xcode7 collectionview