2015-02-24 75 views
-1

我試圖刪除第一個集合視圖單元格。我無法在集合視圖中獲取第一個項目的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? 謝謝。

回答

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]]]; 

}

+0

這是賓果遊戲! – Andy 2015-02-24 20:01:09

相關問題