iOS development: Didselectrowatindexpath: method invalidation Solution
Last Update:2016-01-26
Source: Internet
Author: User
<span id="Label3"></p>Problem description and Resolution Process:<p><p>To reach the effect of clicking the TableView blank area to exit the keyboard, register a UITapGestureRecognizer with the controller to identify the click event, the code is as Follows:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">#pragma</span></span>Mark-set gesture Recognizer<span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008080;"><span style="color: #008080;">3</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Setupgesturerecognizer</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">to <span style="color: #008000;">Create a gesture recognizer Object</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>UITapGestureRecognizer *tap =<span style="color: #000000;"><span style="color: #000000;">[[uitapgesturerecognizer alloc] init];</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">sets the proxy for the gesture Recognizer object to the current controller</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span>Tap.<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span>=<span style="color: #000000;">self <span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Add a gesture recognizer object to the corresponding view</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #000000;"><span style="color: #000000;">[self.view addgesturerecognizer:tap];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">adding a listening method (the corresponding gesture is recognized, the Listener method is Called)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;">[tap addtarget:self action: @selector (tapview)];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Tapview</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">[self.view endediting:yes];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><p><p>But then found that the click TableView cell can not jump (before registering UITapGestureRecognizer is possible), the mode found at all did not enter The-(void) tableView: (uitableview *) TableView didselectrowatindexpath: (nsindexpath *) indexpath method, found after finding the reason is registered gesture recognizer, UITapGestureRecognizer intercepted the touch event, By overriding The-(BOOL) Gesturerecognizer in the uigesturerecognizerdelegate proxy: (uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (uitouch *) The Touch method allows you to customize whether or not to intercept the touch method in a specific case, as Follows:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span># pragma mark-<span style="color: #000000;"><span style="color: #000000;">uigesturerecognizerdelegate</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008080;"><span style="color: #008080;">3</span></span>-(BOOL) gesturerecognizer: (uigesturerecognizer *) gesturerecognizer shouldreceivetouch: (uitouch *<span style="color: #000000;"><span style="color: #000000;">) Touch</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">if Uitableviewcellcontentview (that is, Click tableviewcell), the touch event is not intercepted</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>([nsstringfromclass ([touch.view<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>]) isequaltostring:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Uitableviewcellcontentview</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]) {</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">NO;</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">YES;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span>}</pre>Summarize:<p><p>At present, in my experience, if the occurrence of UITableViewCell can not respond to click events First consider the following two reasons:</p></p> <ul> <ul> <li>The proxy for the UITableView is not set for the current controller;</li> <li>If the gesture recognizer is registered, it may be a problem with the responder chain, and Uigesturerecognizer intercepts the touch event.</li> </ul> </ul><p><p>iOS development: Didselectrowatindexpath: method invalidation Solution</p></p></span>