Delete Cell from UICollectionView Without Reloading
You can try
var toDele = [IndexPath]()
if let tabId = dat["tabId"] as? Int, let resId = dat["resId"] as? Int {
for (index,item) in self.data?.enumerated() {
if item.tabId == tabId {
toDele.append(IndexPath(item:index,section:0))
}
}
for item in toDele {
self.data?.remove(at:item.item)
}
collectionView.deleteItems(at:toDele )
}
or if you don't have duplicates
if let tabId = dat["tabId"] as? Int, let resId = dat["resId"] as? Int {
if let ind = self.data?.firstIndex(where:{ $0.tabId == tabId }) {
self.data?.remove(at:ind)
collectionView.deleteItem(at:IndexPath(item:ind,section:0))
}
}
UICollectionView Cell Deletion
I think your custom layout manager could actually well be the issue.
When you delete the cell from the collectionView you need to invalidate the existing layout, otherwise the layout manager will simply give you the same array of attributes in the cache that it originally calculated - including the layout for the cell that you have deleted, hence the error.
Once you have deleted the relevant entry from your data source, call cache.removeAll(), then call prepareLayout() again to refresh the layout cache. After that, call collectionView.reloadData() (and perhaps also layoutSubviews() and setNeedsDisplay) and that should fix your problem. notice you don't actually need to delete a cell as the collectionView will automatically load in what it needs from the dequeued stack of cells.
Let me know how you get on. All best.
Need To delete cell from UICollectionView
To delete cells from a collectionView you can remove them from the data source and reload the collectionView. You want to make sure you keep your data source in sync with what is on the screen. Basically, keep your data source how you want it and call [collectionView reloadData].
- (void)didTapDeleteCellBtn:(UIButton *)deleteCellBtn {
UICollectionViewCell *cell = (UICollectionViewCell *)deleteCellBtn.superview.superview; // Make sure this gets the right cell for the button
NSIndexPath *indexPath = [self.customCollectionView indexPathForCell:cell];
id item = self.feedItems[indexPath.row];
NSMutableArray *updatedFeedItems = [self.feedItems mutableCopy];
[updatedFeedItems removeObject:item];
self.feedItems = [NSArray arrayWithArray:updatedFeedItems];
[self.customCollectionView reloadData];
}
How to delete a indiviudal collection view cell via button inside that cell using a tag
You need to delete the item from the dataSource/core data and then reload the collection view.
@objc func elete(_ sender:UIButton){
itemName.remove(at: sender.tag) // this removes from array, but you should also remove from core data.
collectionView.reloadData()
}
Empty all cells from UICollectionView
Turns out I can use deleteSections
and pass a NSIndexSet
through to it, making a range of 0,0
and it'll delete the one and only section.
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 0)];
[self.collectionView deleteSections:indexSet];
I could probably just use indexSetWithIndex
but when I did my app crashed.
Related Topics
How to Implement Localization in Swift Ui
Always Stop in App Delegate After Enabling All Exceptions Break Point
How to Add a Character at a Particular Index in String in Swift
Detect and Use Optional External C Library at Runtime in Objective-C
How to Change the Development Language in Xcode Before Internationalizing My App
Custom Init of Uiviewcontroller from Storyboard
How to Get Change in Network Connection Notification from iOS Reachability Class
How to Set the Cornerradius of a Uistackview
Swift: Save Video from Nsurl to User Camera Roll
Change App Language in iOS Without Restarting the App
Swift: Equivalent Objective-C Runtime Class
Can Swift Closures Be Set to a Default Value When Used as a Parameter in a Function
How to Write Application Logs to File and Get Them
Uiscrollview Zooming with Auto Layout