从集合视图中删除特定单元格

问题描述:

我试图删除第一个集合视图单元格。我无法在集合视图中获取第一个项目的indexPath。这是我到目前为止。从集合视图中删除特定单元格

-(void)removeFirstItemFromCollectionView 
{ 
    NSLog(@"%i", [self.hostQueue count]); 
    [self.collectionView performBatchUpdates:^{ 

     NSIndexPath * firstIndexPath = [self.collectionView indexPathForCell:(CollectionViewCell *)[self.collectionView viewWithTag:0]]; 
     NSArray *indexPaths = @[firstIndexPath]; 

     //delete item from hostQueue 
     [self.hostQueue removeObjectAtIndex:0]; 

     // Now delete the items from the collection view. 
     [self.collectionView deleteItemsAtIndexPaths:indexPaths]; 
    } completion:nil]; 
} 

我越来越崩溃有:

NSIndexPath * firstIndexPath = [self.collectionView indexPathForCell:(CollectionViewCell *)[self.collectionView viewWithTag:0]]; 

在我的数据源我设置单元格的标签等于indexPath.row。我想这不起作用?我应该如何获得第一个indexPath? 谢谢。

你并不需要调用indexPathForCell因为你知道你要删除的第一个单元格 - 指数0。您可以使用NSIndexPath类方法indexPathForItem:inSection:创建直接索引路径 -

- (void)removeFirstItemFromCollectionView 
{ 
    NSLog(@"%i", [self.hostQueue count]); 
    //delete item from hostQueue 
    [self.hostQueue removeObjectAtIndex:0]; 

    // Now delete the items from the collection view. 
    [self.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:0]]]; 

}

+0

这是宾果游戏! – Andy 2015-02-24 20:01:09