简体   繁体   中英

Touch events of Scrollview in subview of UITableviewCell in iOS

I have tried searching over the internet about this problem but not able to come up with the solution that solves my problem.

I have make a subclass of UITableViewCell in which I added UIScrollView as a subview, now I want scroll gestures to be listened by scrolView and single tap gesture is listened by UItableView .
Also using this layout is against the HIG of Apple

This is an old question but since I didn't really find a good answer I figured I'd offer one. This approach captures the tap events on both the UIScrollView (contained 'within' the table cell) AND tap events on the UITableView cells directly, and passes them along to the UITableViewDelegate. It also allows you to scroll your UIScrollView content without passing the scroll events along. Doesn't require any subclassing.

  1. Within ViewDidLoad, instantiate a UITapGestureRecognizer on the UITableView
 // Capture taps on scrollView fields and pass along to tableView let tap = UITapGestureRecognizer(target: self, action: "tapIntercept:") tableView.addGestureRecognizer(tap) 
  1. Create a callback function for the selector above
 // Get the location of the table cell beneath the scrollView and pass the event off to the tableView func tapIntercept(recognizer: UIGestureRecognizer) { let point = recognizer.locationInView(tableView) if let indexPath = tableView.indexPathForRowAtPoint(point) { tableView(tableView, didSelectRowAtIndexPath: indexPath) } } 

If the scroll view is a subview of the table view, you can capture the gesture and send it to its parent view like this:

   - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self.nextResponder touchesBegan:touches withEvent:event];
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM