UITableView Scroll Detection

I have subclassed UITableView (as KRTableView) and implemented four touch methods (touchsBegan, touchsEnded, touchhesMoved and touchsCancelled) so that I can detect when a touch event is being processed in a UITableView. Essentially, I need to detect when a UITableView scrolls up or down.

However, subclassing UITableView and creating the above methods only detects when scrolling or moving fingers in the UITableViewCell, and not in the entire UITableView.

As soon as my finger moves to the next cell, touch events do nothing.

This is how I subclass UITableView:

#import "KRTableView.h" @implementation KRTableView - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; NSLog(@"touches began..."); } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesMoved:touches withEvent:event]; NSLog(@"touchesMoved occured"); } - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent *)event { [super touchesCancelled:touches withEvent:event]; NSLog(@"touchesCancelled occured"); } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; NSLog(@"A tap was detected on KRTableView"); } @end 

How can I detect when a UITableView scrolls up or down?

+56
objective-c iphone cocoa-touch uitableview
Oct 19 '09 at 9:59
source share
3 answers

You do not need to intercept event methods. Check the docs for the UIScrollViewDelegate protocol and apply the -scrollViewDidScroll: or -scrollViewWillBeginDragging: methods to suit your situation.

+142
Oct 19 '09 at 10:05
source share

I would like to add the following:

If you are working with UITableView , most likely you are already implementing UITableViewDelegate to populate the table with data.

The UITableViewDelegate protocol conforms to UIScrollViewDelegate, so all you have to do is implement the -scrollViewWillBeginDragging and -scrollViewDidScroll directly in your UITableViewDelegate implementation, and they will be called automatically if the implementation class is set as delegated to your UITableView.

If you also want to capture clicks in your table, and not just drag and drop, you can expand and implement your own UITableViewCell and use the touchhesBegan methods: there. By combining these two methods, you can accomplish most of the things you need when the user interacts with the UITableView.

+38
Feb 17 '11 at 12:38
source share

This was my mistake, I tried to call the method before restarting the tableview. The following code helped me solve this problem.

[mytableview reloadData];

[mytableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:btn.tag-1] atScrollPosition:UITableViewScrollPositionTop animated:YES];

+1
Apr 05 '13 at 9:30
source share



All Articles