Select UITableViewCell by UITextView, losing the ability to call the didSelectRowAtIndexPath Delegate?

I have a problem with my choice of UITableViewCell . I am using UITableViewCell with a UITextView . The UITextView object UITextView not edited, but not scrolled, when user interaction is enabled.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell=[[[NSBundle mainBundle]loadNibNamed:@"DemoCell" owner:self options:nil]objectAtIndex:0]; cell. accessoryType=UITableViewCellAccessoryDisclosureIndicator; } if([distanceArray count]){ UILabel *label1=(UILabel*)[cell viewWithTag:1]; label1.text=[locationNameArray objectAtIndex:[indexPath section]]; label1.textColor=[UIColor blueColor]; UILabel *label2=(UILabel*)[cell viewWithTag:2]; [label2 setText:[distanceArray objectAtIndex:[indexPath section]]]; UITextView *tview=(UITextView*)[cell viewWithTag:3]; [tview setText:[discriptionArray objectAtIndex:[indexPath section]]]; return cell; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ DetailedLocationVIew *dView=[[DetailedLocationVIew alloc]initWithNibName:@"DetailedLocationVIew" bundle:nil]; [self.navigationController pushViewController:dView animated:YES]; [dView release]; } 

If I select a cell over a UITextView , the didSelectRowIndexPath delegate didSelectRowIndexPath not call. Besides more than a UITextView object and the selection works fine? Why does selection not work on a UITextView?

+4
source share
5 answers

Use tview.userInteractionEnabled=NO; and he will solve your problem.

If this does not solve your problem, try this.

 for(UIView * cellSubviews in cell.subviews) { cellSubviews.userInteractionEnabled = NO; } 

It userInteractionEnabled over all the child objects inside the cell and sets the userInteractionEnabled property to NO .

+9
source

These suggestions did not work for me in iOS 6. What is work:

In your:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

Add this:

 cell.textView.dataDetectorTypes = UIDataDetectorTypeAll; cell.textView.editable = NO; cell.textView.userInteractionEnabled = YES; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(forwardToDidSelect:)]; cell.tag = indexPath.row; [cell.textView addGestureRecognizer: tap]; 

As well as:

 - (void) forwardToDidSelect: (UITapGestureRecognizer *) tap { [self tableView: self.tableView didSelectRowAtIndexPath: [NSIndexPath indexPathForRow: tap.view.tag inSection: kTextViewCellSection]]; } 

It seems like setting userInteractionEnabled = YES after setting editable = NO. Installing them in XIB does not work.

+7
source

Since the UITextView caught the touch event, you can solve this in two ways:

1) Subclass of UITextView, catch the touch events, then pass it to the superuser using the same method: ex:

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self.superview touchesEnded:touches withEvent:event]; } 

2) Using delegate UITextView:

 - (void)textViewDidBeginEditing:(UITextView *)textView { NSIndexPath *a = [NSIndexPath indexPathForRow:0 inSection:[textView tag]]; [tableview didSelectRowAtIndexPath: } 
+2
source

In my case, I resize the tableViewCell depending on the length of the textView.text , so I added a UIView on top of the textView and set the color to 0.1%white .

0
source

If you want to disable only one view, you can do the following in Swift.

 theSubTextView.isUserInteractionEnabled = false 

It also disables isEditable and isSelectable.

0
source

Source: https://habr.com/ru/post/1412955/


All Articles