如何删除 UICollectionView 上的单元格
How to delete cells on UICollectionView
我在每个单元格上创建了一个按钮,onClick 让用户删除我的 UICollectionView 的单元格,我在其中初始化单元格我使用以下代码将 indexPath.item 传递给按钮方法:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
FidelityProducts *item = _feedItems[indexPath.item];
NSLog(@"index dell'item:%ld", (long)indexPath.item);
cell.deleteProduct.tag = indexPath.item;
NSLog(@"TAG ASSEGNATO AL BOTTONE: %d",cell.deleteProduct.tag);
[cell.deleteProduct addTarget:self action:@selector(DeleteProductFromArray:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
然后在 DeleteProductFromArray
我创建了以下逻辑以删除单元格:
- (void)DeleteProductFromArray:(UIButton *)button {
[_feedItems removeObjectAtIndex:button.tag];
//check for the Column (can be 0 or 1)
if(button.tag % 2 == 0)
{
IndexColumn = 0;
}
else
{
IndexColumn = 1;
}
//check for the Row
if(button.tag > 1)
{
IndexRow = button.tag/2;
}
if(button.tag <= 1)
{
IndexRow = 0;
}
NSLog(@"button column =%d", IndexColumn);
NSLog(@"button row =%d", IndexRow);
[self.customCollectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:IndexColumn inSection:IndexRow]]];
}
现在,我知道这会导致问题,因为当我从我的 NSMutableArray 中删除一个项目时按钮标签没有更新,但我只需要知道为什么我第一次尝试删除数字 1 的单元格和 2 被取消,但如果我点击单元格 > 3 应用程序崩溃并出现以下错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete item 0 from section 2, but there are only 1 sections before the update
我傻了。
所有的按钮逻辑都没有用,我忘了检查部分的数量,它们总是 0。
已更改
[self.customCollectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:IndexColumn inSection:IndexRow]]];
收件人:
[self.customCollectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:button.tag inSection:0]]];
一切都按预期进行..
我在每个单元格上创建了一个按钮,onClick 让用户删除我的 UICollectionView 的单元格,我在其中初始化单元格我使用以下代码将 indexPath.item 传递给按钮方法:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
FidelityProducts *item = _feedItems[indexPath.item];
NSLog(@"index dell'item:%ld", (long)indexPath.item);
cell.deleteProduct.tag = indexPath.item;
NSLog(@"TAG ASSEGNATO AL BOTTONE: %d",cell.deleteProduct.tag);
[cell.deleteProduct addTarget:self action:@selector(DeleteProductFromArray:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
然后在 DeleteProductFromArray
我创建了以下逻辑以删除单元格:
- (void)DeleteProductFromArray:(UIButton *)button {
[_feedItems removeObjectAtIndex:button.tag];
//check for the Column (can be 0 or 1)
if(button.tag % 2 == 0)
{
IndexColumn = 0;
}
else
{
IndexColumn = 1;
}
//check for the Row
if(button.tag > 1)
{
IndexRow = button.tag/2;
}
if(button.tag <= 1)
{
IndexRow = 0;
}
NSLog(@"button column =%d", IndexColumn);
NSLog(@"button row =%d", IndexRow);
[self.customCollectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:IndexColumn inSection:IndexRow]]];
}
现在,我知道这会导致问题,因为当我从我的 NSMutableArray 中删除一个项目时按钮标签没有更新,但我只需要知道为什么我第一次尝试删除数字 1 的单元格和 2 被取消,但如果我点击单元格 > 3 应用程序崩溃并出现以下错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete item 0 from section 2, but there are only 1 sections before the update
我傻了。 所有的按钮逻辑都没有用,我忘了检查部分的数量,它们总是 0。 已更改
[self.customCollectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:IndexColumn inSection:IndexRow]]];
收件人:
[self.customCollectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:button.tag inSection:0]]];
一切都按预期进行..