Question

I have a UITableViewCell which contains a UITextView. I'd like for certain characters within the UITextView to be tappable - which I'm using TextKit to achieve. I've setup a UITapGestureRecognizer on my UITextView, but am using it to determine which glyphs within my text container have been selected.

The issue I'm running into is that I'd like to pass the tap onto the underlying UITableViewCell if non-relevant glyphs are tapped. What's the best way to achieve this?

NSLayoutManager *layoutManager = textView.layoutManager;
CGPoint location = [recognizer locationInView:textView];
location.x -= textView.textContainerInset.left;
location.y -= textView.textContainerInset.top;

NSUInteger characterIndex;
characterIndex = [layoutManager characterIndexForPoint:location
                                       inTextContainer:textView.textContainer
              fractionOfDistanceBetweenInsertionPoints:NULL];

if (characterIndex < textView.textStorage.length)
{
    NSRange range;
    id value = [textView.attributedText attribute:@"applicableCharacters" atIndex:characterIndex effectiveRange:&range];
    if(value)
    {
         // Handle this tap, don't pass to the UITableViewCell
    }
    else
    {
        // Don't handle this tap, let the UITableView call tableView:didSelectRowAtIndexPath:
    }
}
Was it helpful?

Solution

Based on my own comment, but skipping the delegate part as it is not necessary.

Public property on the cell-subclass:

@property (nonatomic, strong) NSIndexPath *indexPath;

Set this from the datasource when returning cell for indexPath...

Inside your else:

UITableView *tableView = (UITableView *) self.superview;
[tableView.delegate didSelectRowAtIndexPath:self.indexPath];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top