After adding uitapgesturerecognizer gesture recognition in iOS, UITableView's didselectrowatindexpath fails

Source: Internet
Author: User

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" >
    1. UITapGestureRecognizer *onetap = [[[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Hidekeyboard) ] Autorelease];
    2. Onetap.delegate = self;
    3. onetap.numberoftouchesrequired = 1;
    4. [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
  1. #pragma mark-uigesturerecognizerdelegate
  2. -(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) Touch
  3. {
  4. //Output Click on the class name of the view
  5. NSLog (@"%@", Nsstringfromclass ([Touch.view class]));
  6. //If Uitableviewcellcontentview (that is, click Tableviewcell), the touch event is not intercepted
  7. if ([Nsstringfromclass ([Touch.view class]) isequaltostring:@"Uitableviewcellcontentview"]) {
  8. return NO;
  9. }
  10. return YES;
  11. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.