Your tap location is the location in the header, not a cell, so it would never match a cell indexPath
.
You could probably set the tag
for the avatar
view to be the section number in viewForHeaderInSection
and then retrieve the section number in handleTapGesture
via sender.view.tag
. For example:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
...
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
avatar.tag = section; // save the section number in the tag
avatar.userInteractionEnabled = YES; // and make sure to enable touches
[avatar addGestureRecognizer:tapGesture];
//avatar is UIImageView and the user interaction is enabled.
[headerView addSubview: aMessageAvatar];
return headerView;
...
}
-(void)handleTapGesture:(UITapGestureRecognizer *)sender
{
NSInteger section = sender.view.tag;
NSLog(@"In section %d", section);
}