user1120008
user1120008

Reputation: 1015

UITableView single clicks

I have a UITableView and UINavigationController, and I'd like to distinguish between two clicks: 1) normal click that selects a row and 2) a click that happens ANYWHERE else on the screen (other than the buttons on the UINavigationController). I wrote this code:

singleTap = [[UITapGestureRecognizer alloc]
                                     initWithTarget:self action:@selector(singleTapHandler:)];
singleTap.numberOfTapsRequired = 1;

The problem with this however is that it overrides the normal clicks that select a row.

Upvotes: 0

Views: 217

Answers (1)

rob mayoff
rob mayoff

Reputation: 385600

I assume you're putting the tap recognizer on either the UIWindow itself, or the window's sole subview. You need to give the tap recognizer a delegate, and that delegate needs to implement gestureRecognizer:shouldReceiveTouch:.

In that method, you want to return NO if the touch is in a button or if the touch is in a table view cell, and YES otherwise. You need to walk up the view hierarchy, starting with the view that the touch landed in, looking for either of those classes.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    for (UIView *view = touch.view; view; view = view.superview) {
        if ([view isKindOfClass:[UIButton class]])
            return NO;
        if ([view isKindOfClass:[UITableViewCell class]])
            return NO;
    }
    return YES;
}

Upvotes: 1

Related Questions