Reputation: 2073
I have a UITableView
with custom UITableViewCells
which contain only a textfield. What I would like to achieve is that the tableview can be scrolled not only by touching the cell itself but also by touching the textfield. The default textfield behaviour to start editing should be preserved. Is there any way to distinguish a scroll gesture on the whole tableview from the tap gesture on the textfield?
I'm not looking for a way to scroll to a specific cell when editing starts but for preserving the default tableview scrolling behaviour.
Regards
Upvotes: 4
Views: 792
Reputation: 21
I have done it in the following way with subclassing the TableView and overriding the touchesShouldCancel method. The delaysContentTouches and canCancelContentTouches seems to be necessary
class MyTableView : UITableView {
override init(frame: CGRect, style: UITableViewStyle) {
super.init(frame: frame, style: style)
self.delaysContentTouches = false
self.canCancelContentTouches = true
}
override func touchesShouldCancel(in view: UIView) -> Bool {
if view is UITextField {
return true
}
return super.touchesShouldCancel(in: view)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Upvotes: 2
Reputation: 7252
Set the delaysContentTouches
property on your tableView
s scrollview to true/YES
OBJECTIVE-C
self.tableView.scrollView.delaysContentTouches = YES;
SWIFT
tableView.scrollView.delaysContentTouches = true
Upvotes: 0