Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking a custom UITableView section header is invoking didSelectRow: on first row of the section

I have a custom viewForHeaderInSection on a UITableView - let's call it HeaderCell (a simple subclass of UITableViewCell) that has the weirdest behaviour:

  • When I click on the section header:

    • it triggers the tableView:didSelectRowAtIndexPath: with the indexPath of the first UITableViewCell of that section. Like I was clicking on the cell, but no. I'm clicking on the header.

The thing is, if I replace that custom HeaderCell onviewForHeaderInSection with a simple UIView, that doesn't happen anymore! I've checked for any actions linked on that xib and its class, couldn't find any addTarget: or any xib action.

Other weird factor: it does not happen on the first HeaderCell (section 0) only happens on section >= 1.

NOTE: I have a quick-fix for this but it's kind of risky for later support

On HeaderCell class, i just need to implement:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    return;
}

This a last resort solution. Anyone has detected this behaviour before?

like image 278
Carlos Ricardo Avatar asked Sep 05 '13 11:09

Carlos Ricardo


3 Answers

You shouldn't be using subclasses of UITableViewCell for headers/footers. Use UITableViewHeaderFooterView (if you can live with >= iOS6) or simply UIView otherwise.

like image 56
Mike Pollard Avatar answered Nov 11 '22 21:11

Mike Pollard


use return cell.contentView instead of return cell if you are using UITableViewCell for header/footer

like image 27
Jitesh Middha Avatar answered Nov 11 '22 19:11

Jitesh Middha


You can do it through setEnableUserInteraction method. Pls refer the code given,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
[cell setUserInteractionEnabled:YES];


if([indexPath row] == 0)
    [cell setUserInteractionEnabled:NO];



return cell;


}

Hope this will help you :)

like image 21
Augustine P A Avatar answered Nov 11 '22 20:11

Augustine P A