Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable scrolling for UITableView of cells with UITextField

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

like image 796
Christoph Avatar asked Sep 19 '25 13:09

Christoph


1 Answers

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")
}

}

like image 84
esjot Avatar answered Sep 22 '25 11:09

esjot