I have a tableview with 8 custom cells. in the 8th cell I added a scrollView with paging enabled so I can show page 1 and page 2 (or 3, 4... 10) without have a very high cell.
The problem is with the scrollView I can't use didSelectRowAtIndexPath because the cell is behind the scrollView so I'm trying to detect scrollView tap (not swipe).
I played with touchesBegan and touchesEnded but they are never called (I know touches work with UIView only, but maybe.....)
Any help is very appreciated.
Thanks, Max
There is also an elegant resolution:
Create a SubClass from UIScrollView and override the following methods
Passing every touch to the superview of the scroll view and then the didSelectRowAtIndexPath will be called.
I found the simplest solution for my needs:
subclass UIScrollView touchesEnded method and post a notification.
In the UITableview add an observer in viewdidAppear (remove it in viewdiddisappear) to call a function that call tableview didSelectRowForIndexPath.
Something like this (swift version)
In your tableView:
The selected answer is correct, but I updated the code based on a bug I was getting.
In the subclassed scroll view add the following code.
If your
self.delegate
is not theUITableViewCell
, than replace that property with a property to your cell.The cell needs to retrieve the cancel touch event during movement to prevent the undesired results. It can be easily reproducible as follows.
didSelectCell
stateAnother point to mention is that order matters! If the
self.delegate
is not called before theself.superview
then the highlighted state wont happen.Solved subclassing both uitableviewcell and uiscrollview.
It worked for my needs. Hope it can help.
Max
myScrollView.h
myScrollView.m
myCell.h
myCell.m
RootViewController.h
RootViewController.m
There is a trick Apple recommends to use in this case, in theirs WWDC 2014 session "Advanced scrollviews" (See Demo starting from 8:10):
That's all what needs to be done, no need to override
touchesBegan:
,touchesMoved:
and others.I used solution based on overriding of
touchesBegan:
,touchesMoved:
,touchesEnded:
andtouchesCancelled:
previously, but sometimes it caused a weird behaviour: when select a certain cell, method-tableView:didSelectRowAtIndexPath:
was called for cell with different indexPath.Solution from Apple has no side effects so far and looks more elegant.