سؤال

I have a UITableView with some UITableViewCells that contain labels with suggestions for text to be inserted into a UITextView. I want that when the user taps on some uitableviewcell with text suggestion, this text will enter the uitextview, but I also want that the user will be able to edit the uitextview by himself.

All was working fine until I wanted to add the ability to tap on the background after start editing the textview, with this resulting in hiding the keyboard (resignFirstResponder). But when I implemented backgroundTap, the calls to didSelectRowAtIndexPath ceased to happen.

How can I solve this, so that when the user taps on some uitableviewcell, the call will be to didSelectRowAtIndexPath instead of to backgroundTap?

My implementation of backgroundTap is:

   -(void)backgroundTap:(UITapGestureRecognizer *)tapGR{
       //...some code here...
   }

In the viewWillAppear I added the line:

[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTap:)]];

Thanks.

هل كانت مفيدة؟

المحلول

The problem is that the tap gesture recognizer eats the taps intended for the table view. See didSelectRowAtIndexPath: not being called.

A gesture recognizer applies not only to the view to which it is attached (self.view in your case) but also to all that view's subviews all the way down the view hierarchy. In this case, that includes everything, including your table view cells.

The solution is to mediate with a gesture recognizer delegate (iOS 5) or with UIView gestureRecognizerShouldBegin: (iOS 6 only).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top