UIButton en viewForHeaderInSection

I want to put an info light button in a section header

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 20;

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];

  UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
  infoButton.frame = CGRectMake(0, 0, 18, 18); // x,y,width,height
  infoButton.enabled = YES;
  [infoButton addTarget:self action:@selector(infoButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

  //Add the subview to the UIView
  [sectionHeaderView addSubview:infoButton];
return sectionHeaderView;
- (void)infoButtonClicked:(id)sender {

The button does not respond. I am not quite sure why. It probably is something easy. Any help would be appreciated.

preguntado el 22 de mayo de 12 a las 21:05

2 Respuestas

I tried your exact same code (copy and pasted) it into a working UITableView class, and it worked just fine. I was able to tap the button and get the debug output. So your code as shown above is fine, and should work. There must be something else broken in your class.

contestado el 22 de mayo de 12 a las 22:05

Thank you melsam. I just tried it in a sample project too. Works fine. I should have done that first. I guess I'll have to scratch my head some more - Kurt

Alright. I have stumbled upon the answer that I will share for any others who may have a similar issue.

I have a UIViewController with two subviews--a UIView and UITableView. In storyboard, I had the UITableView higher up in the document outline view of storyboard. (The left sided view panel of storyboard that you can open with the lower left corner button). I think that means that the UITableView was a lower layer, if you think of it as a stack of layers.

I switched the layer order (made the UIView higher up on the document outline view) and everything is working fine.

contestado el 22 de mayo de 12 a las 22:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.