如何从UICollectionView中删除项目?

时间:2013-04-24 18:12:58

标签: iphone ios uicollectionview uicollectionviewcell

我正在collection view上显示图片。我想删除button click上的每个项目。现在我可以删除didSelect method上的项目。

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [array removeObjectAtIndex:indexPath.row];
    [self.colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
}

我在CollectionCell上放置了按钮,可以找到indexpath.row on click。但是如何删除

-(void)chat:(int)i 
{
    NSLog(@"index path%d",i);
    [array removeObjectAtIndex:i]; // array object is getting removed,how to delete the CollectionviewCell
}

2 个答案:

答案 0 :(得分:3)

如果您确实知道该部分:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[self.colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:[indexPath]]];

答案 1 :(得分:1)

只需reloadData,并确保array中使用了numberOfItemsInSection。这将重新加载dataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [yourArray count];
}

-(void)chat:(int)i 
{
    NSLog(@"index path%d",i);
    [yourArray removeObjectAtIndex:i]; 
     // array object is getting removed,how to delete the CollectionviewCell
    [self.collectionView reloadData];
}