Reputation: 1098
I'm trying to add a single tap gesture to my table view section headers, using the below code. But it doesn't embed the gesture in the returned view. What am i doing wrong here? really appreciate your help.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [tableView headerViewForSection:section];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissKeyBoard)];
[view addGestureRecognizer:singleTap];
return view;
}
Upvotes: 1
Views: 1039
Reputation: 4119
try setting a delegate to the gesture recognizer so that it is recognized together with the scroll view's (table view) gestures:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
set the delegate like this:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissKeyBoard)];
singleTap.delegate = self;
[view addGestureRecognizer:singleTap];
You also need to make sure that the gesture only gets set once since tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section is called every time the header appears/re-appears on screen. The way you have it, you'll end up with multiple gesture recognizers to the header view.
Upvotes: 2