如何逐个删除UICollectionView图像

时间:2014-08-20 07:32:51

标签: ios objective-c iphone xcode ios7

当我按下删除按钮(小红色按钮)时,我希望此图像被删除&更新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 个答案:

答案 0 :(得分:2)

ok为你的mybutton设置一个标签

[mybutton setTag: indexPath.row];

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