Skip to content

Commit 1a53acc

Browse files
Ask if cell should move for gesture location #13.
Credit: Yuki Nagai (@uny)
1 parent 49238b0 commit 1a53acc

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

LPRTableView.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import UIKit
1414
@objc
1515
public protocol LPRTableViewDelegate: NSObjectProtocol {
1616

17+
/// Asks the delegate whether a given row can be moved to another location in the table view based on the gesture location.
18+
///
19+
/// The default is `true`.
20+
@objc optional func tableView(_ tableView: UITableView, shouldMoveRowAtIndexPath indexPath: IndexPath, forDraggingGesture gesture: UILongPressGestureRecognizer) -> Bool
21+
1722
/// Provides the delegate a chance to modify the cell visually before dragging occurs.
1823
///
1924
/// Defaults to using the cell as-is if not implemented.
@@ -107,6 +112,7 @@ extension LPRTableView: UIGestureRecognizerDelegate {
107112
return (rows > 0)
108113
&& (indexPath != nil)
109114
&& canMoveRowAt(indexPath: indexPath!)
115+
&& shouldMoveRowAt(indexPath: indexPath!, forDraggingGesture: longPressGestureRecognizer)
110116
}
111117

112118
}
@@ -117,6 +123,10 @@ extension LPRTableView {
117123
return dataSource?.tableView?(self, canMoveRowAt: indexPath) ?? true
118124
}
119125

126+
fileprivate func shouldMoveRowAt(indexPath: IndexPath, forDraggingGesture gesture: UILongPressGestureRecognizer) -> Bool {
127+
return longPressReorderDelegate?.tableView?(self, shouldMoveRowAtIndexPath: indexPath, forDraggingGesture: longPressGestureRecognizer) ?? true
128+
}
129+
120130
@objc internal func _longPress(_ gesture: UILongPressGestureRecognizer) {
121131
let location: CGPoint = gesture.location(in: self)
122132
let indexPath: IndexPath? = indexPathForRow(at: location)
@@ -410,6 +420,13 @@ open class LPRTableViewController: UITableViewController, LPRTableViewDelegate {
410420
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
411421
}
412422

423+
/// Asks the delegate whether a given row can be moved to another location in the table view based on the gesture location.
424+
///
425+
/// The default is `true`. The default implementation of this method is empty—no need to call `super`.
426+
open func tableView(_ tableView: UITableView, shouldMoveRowAtIndexPath indexPath: IndexPath, forDraggingGesture gesture: UILongPressGestureRecognizer) -> Bool {
427+
return true
428+
}
429+
413430
/// Provides the delegate a chance to modify the cell visually before dragging occurs.
414431
///
415432
/// Defaults to using the cell as-is if not implemented. The default implementation of this method is empty—no need to call `super`.

0 commit comments

Comments
 (0)