2014-02-10 69 views
2

我在我的应用程序中实现了一个集合视图。从集合视图中删除项目后的标记问题

收集单元格包含一个称为删除按钮的项目按钮。 删除一个项目后,标签没有更新,所以,如果我删除了第二个项目,那么它将删除它旁边的标签。

我的代码如下:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"GradientCell"; 
    UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; 

UIButton *btn_close=[UIButton buttonWithType:UIButtonTypeCustom]; 
    [btn_close setFrame:CGRectMake(50, 00, 18, 18)]; 
    [btn_close setBackgroundImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal]; 
    [btn_close addTarget:self action:@selector(delete_image:) forControlEvents:UIControlEventTouchUpInside]; 
    btn_close.tag=indexPath.row; 
    return cell; 
} 

-(void)delete_image:(UIButton*)sender 
{ 
[self.col_view performBatchUpdates:^{ 
      [arr_images removeObjectAtIndex:sender.tag]; 

     NSIndexPath *indexPath =[NSIndexPath indexPathForRow:sender.tag inSection:0]; 
     [self.col_view deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]]; 

    } completion:^(BOOL finished) { 

    }]; 
} 

回答

2

简单的答案是不要使用该行作为你的标签!
您应该在'标签'中保存任何其他信息,以后可以将其转换为行号。例如,您可以将图像保存在“arr_images”中,理想情况下应该存储指向该单元格表示的对象的指针。

而当你想删除你应该使用对象对象重新构建indexpath

xxxx = [arr_images indexOfObject:sender.tag]; 
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:xxxx inSection:0]; 
[self.col_view deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];