2014-04-15 35 views
3

我在uicollectionviewcell中放置了一個按鈕,當按下該按鈕時,它將以編程方式將單元格設置爲選定狀態。在uicollectionview中刪除選中的單元格

- (void) deleteItem:(id)sender 
{ 
    self.selected = YES; 
    [self.cellOptionsDelegate deleteItem]; 
} 

然後委託給uicollectionviewcontroller刪除所選的項目。

- (void) deleteItem 
{ 
    NSArray* selectedItemsIndexPaths = [self.collectionView indexPathsForSelectedItems]; 

    // Delete the items from the data source. 
    [self.taskArray removeObjectAtIndex:[[selectedItemsIndexPaths objectAtIndex:0] row]]; 

    // Now delete the items from the collection view. 
    [self.collectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths]; 

} 

然而,當我得到使用uicollectionview方法indexPathsForSelectedItems選定的項目,它沒有看到,我選擇的項目和列表爲空。我正在使用新聞選擇委託方法的另一個功能,所以我希望按照我上面解釋的內容做一些事情。有沒有辦法讓這項工作或更好的方式通知控制器,單元格中的按鈕被綁定到特定索引路徑上的單元格?

謝謝。

回答

2

只要從細胞的deleteItem方法

- (void) deleteItem:(id)sender 
{ 
    self.selected = YES; 
    [self.cellOptionsDelegate deleteItem:self]; 
} 

派格指針和改變uicollectionviewcontroller的方法

- (void) deleteItem:(UICollectionViewCell*)cellToDelete 
{ 
    NSIndexPath indexPathToDelete = [self indexPathForCell: cellToDelete]; 

    // Delete the items from the data source. 
    [self.taskArray removeObjectAtIndex:[indexPathToDelete row]]; 

    // Now delete the items from the collection view. 
    [self.collectionView deleteItemsAtIndexPaths:@[indexPathToDelete]]; 

} 

不要忘記更新 '(無效)deleteItem:(UICollectionViewCell *)cellToDelete'聲明在您的* .h文件中。

+0

LOL!當你回答謝謝你時,我已經明白了。 – Shawn

+0

這隻會刪除一個單元格。如何一次性刪除多個選中的單元格點擊某個按鈕? –

1

解決通過委託方法發送的單元格,使用此:

NSIndexPath* indexPath = [self.collectionView indexPathForCell:cell]; 
0

動畫:

[self.collectionView performBatchUpdates:^ { 
     [_array removeObject:file];    
     [self.collectionView deleteItemsAtIndexPaths:@[indexPath]]; 
} completion:nil]; 
相關問題