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

时间:2015-02-24 19:47:08

标签: ios objective-c uicollectionview

我试图删除第一个集合视图单元格。我在收集视图中获取第一个项目的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]];

在我的dataSource中,我将单元格的标记设置为等于indexPath.row。我想这不起作用?我应该如何获得第一个indexPath? 谢谢。

1 个答案:

答案 0 :(得分:1)

您不需要调用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]]];

}