1

I am trying to create a UITableViewCell which has a horizontally scrollable UIScrollView. How to make sure that when user touches on the UIScrollView, the UITableViewCell gets selected.

The UITableViewCell has a UIScrollView as a subview as well as some other subviews. When user taps on non-UIScrollView subviews, the cell gets selected as expected. However, when user taps on the UIScrollView, nothing happens.

The UIScrollView has some sub views. The user can horizontally scroll the scrollview as expected.

Is there any way such that, when user flicks through the UIScrollView, the UIScrollView handles the touch event, but when user taps on the UIScrollView, it passes the event to the superview?

Edit - I tried overriding touchesEnded(_:with:) as follows -

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    superview?.touchesEnded(touches, with: event)
}

It did not work

Secondly, I tried adding a UITapGestureRecognizer to the scrollview and check if detects the taps. It does detect the tap. However, in order to select the UITableViewCell, I need to get the respective UITableViewCell for the scrollview, find its indexPath and then using the UITableView select that indexPath.

I am hoping if there is a simpler way to perform what I am trying to achieve.

  • Did you try adding a tap gesture? Did you try overriding `touchesBegan`, `touchesMoved` and `touchesEnded` to detect tap? What did you try? – Brandon Dec 24 '18 at 17:11
  • @Brandon, I have made an edit to my post, where I have listed what I have done. –  Dec 24 '18 at 17:26

1 Answers1

0

I solved the problem by adding a UITapGestureRecognizer to the scrollView.

In the function that gets called when the user taps on the scrollview, I programmatically select the row as follows -

guard let indexPath = tableView.indexPath(for: cell) else { return }
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
tableView(tableView, didSelectRowAt: indexPath)