How to Delete Item from Collection View

How to delete item from collection view?

You should change

self.collectionView.deleteItemsAtIndexPaths([indexPath.item])

to

self.collectionView.deleteItemsAtIndexPaths([indexPath])

deleteItemsAtIndexPaths expects an array of NSIndexPaths, not an array of numbers.

Besides that, if you call deleteItemsAtIndexPaths you don't need a call to reloadData - this will even prevent any animation from happening.

Don't forget to update your data source - the person has to be removed from the people array.

people.removeAtIndex(indexPath.item)

Do this before calling deleteItemsAtIndexPaths.

How do I delete an item in a collection view with a button in the cell?

Closures aren't overcomplicated. Try something like this:

/// the cell
class CollectionCell: UICollectionViewCell {
var deleteThisCell: (() -> Void)?
@IBAction func deletePressed(_ sender: Any) {
deleteThisCell?()
}
}
/// the view controller

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "yourReuseID", for: indexPath) as! CollectionCell
cell.deleteThisCell = { [weak self] in

/// your deletion code here
/// for example:

self?.yourDataSource.remove(at: indexPath.item)

do {
try self?.realm.write {
self?.realm.delete(projects[indexPath.item]) /// or whatever realm array you have
}
self?.collectionView.performBatchUpdates({
self?.collectionView.deleteItems(at: [indexPath])
}, completion: nil)
} catch {
print("Error deleting project from realm: \(error)")
}
}
}

Deleting Items out of collection view

These codes will delete a specific item using the item index. This is working great!

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var visibleRect = CGRect()

visibleRect.origin = myCollectionView.contentOffset
visibleRect.size = myCollectionView.bounds.size

let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
let visibleIndexPath: IndexPath = myCollectionView.indexPathForItem(at: visiblePoint)!

print(visibleIndexPath)

fruitArray.remove(at: visibleIndexPath.row )
myCollectionView.deleteItems(at: [visibleIndexPath])
myCollectionView.scrollToItem(at: IndexPath.init(row: visibleIndexPath.row-1, section: 0), at: UICollectionViewScrollPosition.centeredHorizontally, animated: false)
}

How do I delete collection view cells along with this behaviour?

UICollectionView and UITableView create a default animation which is the one you want when you delete cells from them.
Although, since you are using only one column, I'd suggest that you use a UITableView instead of a UICollectionView.
To remove a row from a tableview with the animation, do the following :

self.yourDataSourceArray.removeValue(at: self.indexPath.row)
self.tableView?.deleteRows(at: [self.indexPath], with: UITableViewRowAnimation.automatic)

If you still want to use a UICollectionView, here's how to do it :

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.yourDataSourceArray.remove(at: indexPath.row)
collectionView.deleteItems(at: [indexPath])
}

How delete multiple selected cells from collection view? (swift)

var _selectedCells = [IndexPath]()

Add Cell Index

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if self.EditToolBar.isHidden == true {
self.performSegue(withIdentifier: "DetailVC", sender: indexPath.item)
} else {
print("EditMode")
if !(_selectedCells.contains(indexPath)) {
_selectedCells.add(indexPath)
print("selectedCells - \(_selectedCells)")
}

}
}

Delete cell Index

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if self.EditToolBar.isHidden == true {
} else {
print("EditMode")

if let index = _selectedCells.index(where: { $0 == indexPath }) {
_selectedCells.remove(at: index)
print("unselectedCells - \(_selectedCells)")

}
}
}

Delete Button Action

@IBAction func DeleteButton(_ sender: UIBarButtonItem) {
// Two Things To make sure of
// 1. Never call reloadData() right after insert/move/deleteRows..., the insert/move/delete operation reorders the table and does the animation
// 2. Call insert/move/deleteRows... always after changing the data source array.

// remove the data from data Source Array you passed ,of selected cells you're trying to delete .

self.collectionView.performBatchUpdates({
self.collectionView.deleteItems(at indexPaths: _selectedCells)
}){
// optional closure
print(“finished deleting cell”)
}

}

How to delete an item from UICollectionView with indexpath.row

-(void)remove:(int)i {

[self.collectionObj performBatchUpdates:^{
[array removeObjectAtIndex:i];
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:i inSection:0];
[self.collectionObj deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];

} completion:^(BOOL finished) {

}];
}

Try this. It may work for you.



Related Topics



Leave a reply



Submit