Transferred from: http://blog.csdn.net/iosevanhuang/article/details/8830499
Today's colleagues in the development process encountered a problem, roughly described as follows:
He used TableView to write a login interface, account number and password two cells added TextField. He registered a uitapgesturerecognizer to identify gestures because he wanted to tableview the keyboard when clicked in the blank space.
Part of the code for registering gestures in Viewdidload is as follows:
[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
- UITapGestureRecognizer *onetap = [[[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Hidekeyboard) ] Autorelease];
- Onetap.delegate = self;
- onetap.numberoftouchesrequired = 1;
- [Self.view Addgesturerecognizer:onetap];
One of the Hidekeyboard methods is omitted, the basic is a few resignfirstresponder.
Then there was a strange phenomenon, click on the "Retrieve password" column when no response!!!
After debugging I found that the delegate method
-(void) TableView: (uitableview *) TableView Didselectrowatindexpath: (nsindexpath*) Indexpath not executed.
The visual reason is that UITapGestureRecognizer intercepted the touch event, causing the Didselectrowatindexpath method to fail to respond.
A workable workaround was found after reviewing the relevant documents: overriding the Uigesturerecognizerdelegate in the
-(BOOL) Gesturerecognizer: (uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) Touch method.
[CPP]View Plaincopy
- #pragma mark-uigesturerecognizerdelegate
- -(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) Touch
- {
- //Output Click on the class name of the view
- NSLog (@"%@", Nsstringfromclass ([Touch.view class]));
- //If Uitableviewcellcontentview (that is, click Tableviewcell), the touch event is not intercepted
- if ([Nsstringfromclass ([Touch.view class]) isequaltostring:@"Uitableviewcellcontentview"]) {
- return NO;
- }
- return YES;
- }
Debug again, this time Didselectrowatindexpath responds, solve the problem of UITapGestureRecognizer gesture intercept touch event!
PS: The author has tested other gestures outside of tap gestures, Pinch, Pan, Swipe, rotation did not produce similar problems, and tap and longpress produced such problems. Thank Doralink remind, write technical blog or to write a comprehensive.
After adding uitapgesturerecognizer gesture recognition in iOS, UITableView's didselectrowatindexpath fails