Kurt
Kurt

Reputation: 855

UIButton in 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 {
  NSLog(@"infoButtonClicked");
}

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

Upvotes: 1

Views: 1614

Answers (2)

Kurt
Kurt

Reputation: 855

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.

Upvotes: 0

melsam
melsam

Reputation: 4977

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.

Upvotes: 2

Related Questions