Christoph
Christoph

Reputation: 2073

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

Upvotes: 4

Views: 792

Answers (2)

esjot
esjot

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

Beau Nouvelle
Beau Nouvelle

Reputation: 7252

Set the delaysContentTouches property on your tableViews scrollview to true/YES

OBJECTIVE-C

self.tableView.scrollView.delaysContentTouches = YES;

SWIFT

tableView.scrollView.delaysContentTouches = true

Upvotes: 0

Related Questions