如何将 UICollectionView Image 一张一张删除

Posted

技术标签:

【中文标题】如何将 UICollectionView Image 一张一张删除【英文标题】:How to delete UICollectionView Image one by one 【发布时间】:2014-08-20 07:32:50 【问题描述】:

当我按下删除按钮(红色小按钮)时,我希望这张图片删除并更新 UICollectionView。我为这个 collectionView 编写了这段代码。

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath


_cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
_mainindex=indexPath;
UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 70, 70)];
UIImage *img=_selectedAssetArray[_mainindex.row];
imageview.image=img;
imageview.userInteractionEnabled=YES;
[_cell.contentView addSubview:imageview];

UIButton *mybutton=[[UIButton alloc]initWithFrame:CGRectMake(50, -5, 30, 30)];
[mybutton setImage:[UIImage imageNamed:@"closeButton2.png"] forState:UIControlStateNormal];
[mybutton addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
[imageview addSubview:mybutton];
return _cell;

在 didSelectItemAtIndexPath 中

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

  _mainindex=indexPath;
CropViewController *cropview=[[CropViewController alloc]initWithNibName:@"CropViewController" bundle:nil];
cropview.AftersaveArray=_selectedAssetArray;
cropview.CropIndex=_mainindex;
cropview.cropImage=[_selectedAssetArray objectAtIndex:indexPath.row];
cropview.CropImagedelegate=self;
[self presentViewController:cropview animated:YES completion:nil];

所以,我想在按下按钮时删除图像。

【问题讨论】:

【参考方案1】:

ok 像这样为你的 mybutton 设置一个标签

[mybutton setTag: indexPath.row];

-(void) delete :(id)sender
    UIButton *btn = (UIButton *)sender;
    [_selectedAssetArray removeObjectAtIndex:btn.tag];
    //reload your collectionview here

【讨论】:

以上是关于如何将 UICollectionView Image 一张一张删除的主要内容,如果未能解决你的问题,请参考以下文章

如何将 UICollectionView 添加到 inputAccessoryView

如何将 UIPageControl 连接到 UICollectionView (Swift)

如何将 UICollectionView 的大小更改为屏幕大小的一个因素?

如何将背景图像添加到 UICollectionView 将滚动和缩放单元格

如何将 Plist 文件与 UITableView / UICollectionView 一起使用?

如何将 UICollectionView Image 一张一张删除