Custom methods of selection of UITableViewCells
Introduction#
Advance ways to manage selections of UITableViewCell. Examples when simple didSelect...
form UITableViewDelegate
is not enough to achieve something.
Distinction between single and double selection on row.
An example of implementation which give a possibility to detect if user single or double tap on UITableViewCell.
override func viewDidLoad() {
viewDidLoad()
let doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap(sender:)))
doubleTapGestureRecognizer.numberOfTapsRequired = 2
tableView.addGestureRecognizer(doubleTapGestureRecognizer)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(sender:)))
tapGestureRecognizer.numberOfTapsRequired = 1
tapGestureRecognizer.require(toFail: doubleTapGestureRecognizer)
tableView.addGestureRecognizer(tapGestureRecognizer)
}
func handleTapGesture(sender: UITapGestureRecognizer) {
let touchPoint = sender.location(in: tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
print(indexPath)
}
}
func handleDoubleTap(sender: UITapGestureRecognizer) {
let touchPoint = sender.location(in: tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
print(indexPath)
}
}