gpt4 book ai didi

ios - 从 Collection View 中删除项目后的标签问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:50 26 4
gpt4 key购买 nike

我在我的应用中实现了一个 Collection View 。

Collection 单元格包含一个称为项目删除按钮的按钮。删除一个项目后,标签不会更新,因此,如果我删除第二个项目,那么它将删除它旁边的一个标签。

我的代码如下:

- (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) {

}];
}

最佳答案

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

当你想删除对象时,你应该使用该对象重新构建索引路径

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

关于ios - 从 Collection View 中删除项目后的标签问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21677577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com