سؤال

I have a TTStyledTextLabel inside a UITableViewCell. Clicking on the cell navigates to a new view controller, so I can't disable selection, but when I click the TTStyledTextLabel, the UITableViewCell is also selected. Any thoughts on clicking the TTStyledTextLabel without also selecting the table view cell?

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

المحلول

Just subclass TTStyledTextLabel and override the following two methods:

  • (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
  • (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event

just like this:

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch* touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    point.x -= _contentInset.left;
    point.y -= _contentInset.top;

    TTStyledBoxFrame* frame = [_text hitTest:point];
    if (frame) {
        [self setHighlightedFrame:frame];
    }
}

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
    TTTableView* tableView = (TTTableView*)[self ancestorOrSelfWithClass:[TTTableView class]];
    if (!tableView) {
        if (_highlightedNode) {
            // This is a dirty hack to decouple the UI from Style. TTOpenURL was originally within
            // the node implementation. One potential fix would be to provide some protocol for these
            // nodes to converse with.
            if ([_highlightedNode isKindOfClass:[TTStyledLinkNode class]]) {
                TTOpenURL([(TTStyledLinkNode*)_highlightedNode URL]);

            } else if ([_highlightedNode isKindOfClass:[TTStyledButtonNode class]]) {
                TTOpenURL([(TTStyledButtonNode*)_highlightedNode URL]);

            } else {
                [_highlightedNode performDefaultAction];
            }
            [self setHighlightedFrame:nil];
        }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top