Reputation: 69
I want to delete cells in a UICollectionView, which have a certain ID. So the cell can be in the middle of the view.
To do so I remove the item from my cell array and also remove the indexPath. The indexPath has to be retrieved first. This is not possible!
Somehow the cells which are not visible / hidden (below the current view, have to be scrolled up) can not be deleted, as they turn out to be NIL.
How can I access a certain cell (with an ID) and delete it?
let cellIdToFeedItem_ToDelete = [ String : FeedItem ]() // here are the item to be deleted
var indexPathsToRemove = [ IndexPath ]()
var indexPathsAllActive = [ IndexPath ]() // Will be filled below
// --- retrieving all indexPaths
if let nrSections = self.collectionView?.numberOfSections
{
for s in 0..<nrSections
{
if let v = self.collectionView?.numberOfItems( inSection: s )
{
for i in 0..<v
{
let indexPathTMP = IndexPath( item: i, section: s )
indexPathsAllActive.append( indexPathTMP )
}
}
}
}
// --- delete the items with the same
for indexPath in indexPathsAllActive
{
// #### FOR HIDDEN CELLS THIS IS 'NIL'
let cell = self.collectionView?.cellForItem( at: indexPath ) as? FeedCollectionViewCell
if let cellID = cell?.id
{
if let _ = cellIdToFeedItem_ToDelete[ cellID ]
{
if let index = feedItemsActive_.firstIndex( where: {
(i) -> Bool in
i.id == cellID
} )
{
feedItemsActive_.remove( at: index )
indexPathsToRemove.append( indexPath )
}
}
}
}
self.collectionView?.deleteItems( at: indexPathsToRemove )
Upvotes: 0
Views: 458
Reputation: 4918
Remove the items from your datasource (indexPathsAllActive I assume?) then reload your collectionView to show the changes.
Without animation:
collectionView.reloadData()
With animations:
collectionView.performBatchUpdates{ }
Here's how you'd use performBatchUpdates with your code: (If you have one section)
collectionView?.performBatchUpdates({
var targetIndexPaths: [IndexPath] = []
for ( cellID, _ ) in cellIdToFeedItem_ToDelete
{
if let index = feedItemsActive_.firstIndex( where: { $0.id == cellID } )
{
feedItemsActive_.remove( at: index )
targetIndexPaths.append(IndexPath(item: index, section: 0))
}
}
collectionView?.deleteItems(at: targetIndexPaths])
}, completion: nil)
Upvotes: 3
Reputation: 69
This did the job. But without animation though:
for ( cellID, _ ) in cellIdToFeedItem_ToDelete
{
if let index = feedItemsActive_.firstIndex( where: {
(i) -> Bool in
i.id == cellID
} )
{
feedItemsActive_.remove( at: index )
}
}
self.collectionView?.reloadData()
Upvotes: 0