SebastianThiebaud / STTweetLabel

Deprecated - A UILabel with #hashtag @handle and links tappable
MIT License
654 stars 137 forks source link

Preventing didSelectRowAtIndexPath #103

Closed ghost closed 10 years ago

ghost commented 10 years ago

Hi!

First of all, thanks for your great piece of code. Currently I am dealing with a strange problem: I added STTweetLabel (the latest version) to my custom table view cell. After I click on a hash tag or another keyword, the STTweetLabel block is called, but the event is also forwarded to the underlying UITableView which calls didSelectRowAtIndexPath. But I want to prevent this behavior. If the user clicks on a hashtag, the UITableView delegate shouldn't be called. Can you help me to achieve this? Thanks in advance.

Here is the code for setting the detection block:

[textLabel setDetectionBlock:^(STTweetHotWord hotWord, NSString *string, NSString *protocol, NSRange range) {
    switch(hotWord) {
    case STTweetHashtag:
        if(self.delegate != nil) {
            [self.delegate hashtagClicked:string];
        }
        break;

    default:
        break;
    }
}];

Best regards Nekro

gituser87 commented 10 years ago

I'm facing the exact same issue. Has anyone managed to fix this?

gituser87 commented 10 years ago

Ok so I got an answer from SO that worked for me. You have to remove the following lines from STTweetLabel.m

[super touchesBegan:touches withEvent:event];
[super touchesMoved:touches withEvent:event];
[super touchesEnded:touches withEvent:event];

The above results in the touch being passed up the responder chain where its acted upon by the tableview.

SebastianThiebaud commented 10 years ago

Interesting. Gonna take a look at this.

bobspryn commented 10 years ago

:+1:

bobspryn commented 10 years ago

@gituser87 That works, but it then entirely removes the ability to select a cell. I'm trying to support both behaviors. Hmmm.

ghost commented 10 years ago

Yes, fortunately I have enough space and other items in my UITableViewCell that can be selected to move to detail. The label control is always meant to launch a different functionality. Would be great to have the best of both world's though :)

ghost commented 10 years ago

Hey @SebastienThiebaud @gituser87 and @sprynmr I already solved the issue for me. Perhaps you can implement this solution into your code. All events are still forwarded, EXCEPT the case the user clicked on a hot word. For this case, the hot word handle block will be called. I did it like this:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    ...
    BOOL hotwordFound = NO;

    for (id obj in _rangesOfHotWords) {
        NSRange range = [[obj objectForKey:@"range"] rangeValue];

        if (charIndex >= range.location && charIndex < range.location + range.length) {
            _detectionBlock((STTweetHotWord)[[obj objectForKey:@"hotWord"] intValue], [_cleanText substringWithRange:range], [obj objectForKey:@"protocol"], range);

            hotwordFound = YES;
            break;
        }
    }

    if(!hotwordFound) {
        [super touchesEnded:touches withEvent:event];
    }
}

Perhaps this helps a bit. And thanks for your great control ;)

bobspryn commented 10 years ago

@Nuker I tried this at one point. The problem is then your table view cell can be stuck highlighted because it never receives a touchedEnded event. Are you not see in this?

ghost commented 10 years ago

@sprynmr Currently I do not have this problem because I deactivated the cell selection animation. Perhaps this is a solution (I did not try this): create a private method to determine if a hotword has been selected. Now use this method to determine if the event should be forwarded or not, just like I did above. This way all events will still be forwarded except those in which a hotword has been selected

ghost commented 10 years ago

@SebastienThiebaud I think I'll implement my proposed solution and post the results today evening (CET). As far as I can see it should work and should solve the problems ;)

magnett commented 10 years ago

@Nuker Could you please provide the whole method, because i still can't intercept the hot word calls

ghost commented 10 years ago

Hi @ll!

Sorry for the delay, but here is a fixed class. Just use this and all events will only be forwarded if NO hot word has been touched.

@SebastienThiebaud Perhaps you can use this implementation for your component?

http://tny.cz/14be774a

SebastianThiebaud commented 10 years ago

Hi @Nuker

Thanks for your work, can you submit a pull request? Thank you.

SebastianThiebaud commented 10 years ago

Fixed with #106