Skip to content

Commit

Permalink
Add shouldDelete*At methods to swipe cell delegates
Browse files Browse the repository at this point in the history
Currently both `SwipeCollectionViewCell` and `SwipeTableViewCell`
automatically delete the cells on
`swipeController(:didDeleteSwipeableAt:)` and don't allow any
customization, like what's allowed in other similar methods.

While on most scenarios the default behavior is desired, there are some
scenarios where users might want to control how the deletion is
effectively made on the container (CollectionView or TableView). Some
examples are:
- Data-driven setups that use diffing
- Customize the deletion animation (table view)

To achieve that, a new delegate method was added to both the
`SwipeCollectionViewCellDelegate` and `SwipeTableViewCellDelegate` to
allow customizing the deletion on the container, having the current
implementations as the default in protocol extensions.

## Changes

- Add new `collectionView(:shouldDeleteItemAt:)` delegate method to
`SwipeCollectionViewCellDelegate`, and invoke it on
`SwipeCollectionViewCell.swipeController(:didDeleteSwipeableAt:)`.

- Add new `tableView(:shouldDeleteRowAt:)` delegate method to
`SwipeTableViewCellDelegate` and invoke it on
`SwipeTableViewCell.swipeController(:didDeleteSwipeableAt:)`.
  • Loading branch information
p4checo committed May 5, 2021
1 parent 6d632bd commit d595356
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Source/SwipeCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ extension SwipeCollectionViewCell: SwipeControllerDelegate {
}

func swipeController(_ controller: SwipeController, didDeleteSwipeableAt indexPath: IndexPath) {
collectionView?.deleteItems(at: [indexPath])
guard let collectionView = collectionView, let indexPath = collectionView.indexPath(for: self) else { return }

delegate?.collectionView(collectionView, shouldDeleteItemAt: indexPath)
}
}

13 changes: 13 additions & 0 deletions Source/SwipeCollectionViewCellDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public protocol SwipeCollectionViewCellDelegate: class {
- parameter orientation: The side of the item.
*/
func collectionView(_ collectionView: UICollectionView, didEndEditingItemAt indexPath: IndexPath?, for orientation: SwipeActionsOrientation)

/**
Tells the delegate that the collection view should delete an item following a swipe action.
- parameter collectionView: The collection view object providing this information.
- parameter indexPath: The index path of the item.
*/
func collectionView(_ collectionView: UICollectionView, shouldDeleteItemAt indexPath: IndexPath)

/**
Asks the delegate for visibile rectangle of the collection view, which is used to ensure swipe actions are vertically centered within the visible portion of the item.
Expand All @@ -84,6 +93,10 @@ public extension SwipeCollectionViewCellDelegate {
func collectionView(_ collectionView: UICollectionView, willBeginEditingItemAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) {}

func collectionView(_ collectionView: UICollectionView, didEndEditingItemAt indexPath: IndexPath?, for orientation: SwipeActionsOrientation) {}

func collectionView(_ collectionView: UICollectionView, shouldDeleteItemAt indexPath: IndexPath) {
collectionView.deleteItems(at: [indexPath])
}

func visibleRect(for collectionView: UICollectionView) -> CGRect? {
return nil
Expand Down
4 changes: 3 additions & 1 deletion Source/SwipeTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ extension SwipeTableViewCell: SwipeControllerDelegate {
}

func swipeController(_ controller: SwipeController, didDeleteSwipeableAt indexPath: IndexPath) {
tableView?.deleteRows(at: [indexPath], with: .none)
guard let tableView = tableView, let indexPath = tableView.indexPath(for: self) else { return }

delegate?.tableView(tableView, shouldDeleteRowAt: indexPath)
}
}
14 changes: 14 additions & 0 deletions Source/SwipeTableViewCellDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ public protocol SwipeTableViewCellDelegate: class {
- parameter orientation: The side of the cell.
*/
func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?, for orientation: SwipeActionsOrientation)


/**
Tells the delegate that the table view should delete a row following a swipe action.
- parameter tableView: The table view object providing this information.
- parameter indexPath: The index path of the row.
*/
func tableView(_ tableView: UITableView, shouldDeleteRowAt indexPath: IndexPath)

/**
Asks the delegate for visibile rectangle of the table view, which is used to ensure swipe actions are vertically centered within the visible portion of the cell.
Expand All @@ -85,6 +95,10 @@ public extension SwipeTableViewCellDelegate {
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) {}

func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?, for orientation: SwipeActionsOrientation) {}

func tableView(_ tableView: UITableView, shouldDeleteRowAt indexPath: IndexPath) {
tableView.deleteRows(at: [indexPath], with: .none)
}

func visibleRect(for tableView: UITableView) -> CGRect? {
return nil
Expand Down

0 comments on commit d595356

Please sign in to comment.