18
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{

 if(section != 0) {

  UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease];
  view.backgroundColor = [UIColor redColor];

  return view;

 } else {
  return tableView.tableHeaderView;
 }

}

This is my implementation of viewForHeaderInSection but whatever frame I make it's always showing me the same red frame. Do you see any problem with my code?

Image:

enter image description here

UPDATE:

Mhm now my red block is higher but my first tableHeader is now somehow hidden. The first one was implemented with the titleForHeaderInSection. I thought I just implement the height of the tableHeader height but that doesnt work

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section == 1)
    return 30;
else
    return tableView.tableHeaderView.frame.size.height;
}
Guru
  • 21,652
  • 10
  • 63
  • 102
gabac
  • 2,062
  • 6
  • 21
  • 30

1 Answers1

43

You need to implement this delegate method

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

In your case, you can simply return 30;.


Also, you are leaking view!

Your [view release] happens after the return. But as soon as the return happens the method execution is aborted and your release is never called.

So you want this instead

UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease];

And get rid of the explicit release down below.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • thx, you know why they dom't take rame height? dont see why I have to set the height in a extra delegate method... – gabac Mar 15 '10 at 20:15
  • I don't know why Apple did it this way. It's a little silly. But I think they intended the header frame to *always* span the entire width of the table view. So the only size variable left to mess with is the height. – Alex Wayne Mar 15 '10 at 20:18
  • thx about the hint of the autorelease. But now I have another problem, maybe you can help me another time? – gabac Mar 15 '10 at 20:23
  • This is a different issue and it's a little unclear. Post a new question with only the relevant code and a new image showing the problem. – Alex Wayne Mar 15 '10 at 22:00
  • thx a lot. Did that: http://stackoverflow.com/questions/2450957/height-of-tableheaderview-seems-to-be-0 – gabac Mar 15 '10 at 22:38