Ryan
Ryan

Reputation: 4884

UITextView on UITableViewCell

I have a tricky problem here.

A UITextView is a subview of `UITableViewCell.

The UITextView receives touch event and tableView: didSelectRowAtIndexPath: isn't invoked.

I know I can get the tableView's event if I set userInteraction:NO to UITextView. However, the UITextView's content is NSAttributedString and the string has NSLinkAttributeName attribute. If I set userInteraction to false, I can't get the textView:shouldInteractWithURL:inRange:.

Is there any good way to be enabled both events together?

Upvotes: 0

Views: 551

Answers (2)

Miknash
Miknash

Reputation: 7948

Since you are trying to get a cell why don't user superview ?

For instance, when you select your textBox, you can say something like

- (void)textViewDidBeginEditing:(UITextView *)textView
{
   UITableViewCell *cell = (UITableViewCell*)textView.superview.superview;
}

Note that you need it twice, once for content view and next for tableview cell.

Here is reference for UITableView hierarchy: http://www.curiousfind.com/blog/646

Also, take care when you are dealing with ios8 since I think they added additional layer. Because of that you could do something like this:

-(UITableViewCell*)getCellForTextView:(UIView*)searchForView
{
    if(search isKindOfClass:[UITableViewCell class]){
        return search;
    }
    if(!search)
       return NULL;
    return [self getCellForTextView:search.superview];
}

and in you function you can do:

- (void)textViewDidBeginEditing:(UITextView *)textView
    {
       UITableViewCell *cell = [self getCellForTextView:textView];
       if(cell) 
       {
          //do stuff
       }
       else 
       {
            //it is null! handle this somehow
       }
    }

Upvotes: 0

YYfim
YYfim

Reputation: 1412

You can create a subClass of the UITextView and add two variables

@property (nonatomic) NSInteger row;
@property (nonatomic) NSInteger section;

that will hold the section number and row number. With these properties you can use delegation/KVO to the tell the viewController that a cell in (row:x & section:y) was selected.

EDIT

This edit is updated from @Daniel Galasko comment.

A better way to solve this is using the method indexPathForRowAtPoint: like so:

set the viewController to be the UITextView's delegate, and in the dekegate method

- (void)textViewDidBeginEditing:(UITextView *)textView
{
   CGPoint position = [textView convertPoint:CGPointZero toView: <yourtableView> ];
   NSIndexPath *indexPath = [<your tableView> indexPathForRowAtPoint: position];
} 

Upvotes: 1

Related Questions