0

I'm getting some odd behavior with my UITableView.

There's spacing between my UITableView Header and the first cell. Any idea why this is?

#define kDefaultTableViewSectionHeight 50

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.posts.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kDefaultTableViewCellHeight;
}

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

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

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kDefaultTableViewSectionHeight)];
    UIButton *hot = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, 0, 100, kDefaultTableViewSectionHeight)];
    UIButton *new = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-100, 0, 100, kDefaultTableViewSectionHeight)];
    UIView *underline = [[UIView alloc] initWithFrame:CGRectMake(new.frame.origin.x, view.frame.size.height-10, 70, 1)];
    UIView *border = [[UIView alloc] initWithFrame:CGRectMake(0, view.frame.size.height-1, view.frame.size.width, 1)];

    [border setBackgroundColor:[UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:1]];

    self.underline = underline;

    if (self.new)
        [underline setCenter:CGPointMake(new.center.x, underline.center.y)];
    else
        [underline setCenter:CGPointMake(hot.center.x, underline.center.y)];

    [underline setBackgroundColor:[UIColor whiteColor]];
    [view setBackgroundColor:[UIColor blackColor]];

    [hot setTitle:@"Hot" forState:UIControlStateNormal];
    [hot setTag:2];
    [hot setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [hot addTarget:self action:@selector(selectQueryType:) forControlEvents:UIControlEventTouchUpInside];
    [hot.titleLabel setFont:[UIFont fontWithName:@"Avenir-Heavy" size:20.0]];

    [new setTitle:@"New" forState:UIControlStateNormal];
    [new setTag:1];
    [new setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [new addTarget:self action:@selector(selectQueryType:) forControlEvents:UIControlEventTouchUpInside];
    [new.titleLabel setFont:[UIFont fontWithName:@"Avenir-Heavy" size:20.0]];

    [view addSubview:new];
    [view addSubview:hot];
    [view addSubview:underline];
    [view addSubview:border];
    return view;
}

I've tried the following code to fix the issue, but this just reduces the space between the top of the UITableView and the UINavigationBar:

    /*
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7){
        self.postTableview.contentInset = UIEdgeInsetsMake(-8, 0, 0, 0);
    } */

but this just reduces the spacing between the top of the UITableView and the UINavigationbar.

enter image description here

Apollo
  • 8,874
  • 32
  • 104
  • 192
  • 2
    Here you have another question where you probably will find an answer for this http://stackoverflow.com/questions/18880341/why-is-there-extra-padding-at-the-top-of-my-uitableview-with-style-uitableviewst – Claudio Redi Jun 02 '15 at 20:30
  • @ClaudioRedi my problem is different, since the spacing *starts* at the header and *ends* at the top of the first cell. – Apollo Jun 02 '15 at 20:41
  • one of the answers talks about setting 0 for section header. I recommend you to take a deeper look since there are many different answers. – Claudio Redi Jun 02 '15 at 20:43
  • are you using delegate for setting the headerView? have you checked the height returned by: `- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section? ` `kDefaultTableViewSectionHeight *50*` from the image i think it's at around 35 or something try declaring lesser value to `kDefaultTableViewSectionHeight` – 0yeoj Jun 03 '15 at 09:17
  • @0yeoj Yes it's in my the code of my question above – Apollo Jun 03 '15 at 13:52
  • under `-(void)viewDidLoad` you have self.automaticallyAdjustsScrollViewInsets = NO; ? – 0yeoj Jun 04 '15 at 02:16
  • @0yeoj yep, tried this and it has no affect – Apollo Jun 04 '15 at 02:23
  • I currently testing your code above and it is working fine without any modification, hmm.. – 0yeoj Jun 04 '15 at 02:48

1 Answers1

0

I think you are confusing the tableHeaderView property with the section header view. The delegate method tableview:viewForHeaderInSection: that you are calling sets the header of the section rather than the header of the UITableView itself. In your -numberOfSectionsInTableView: you are only returning 1 section, so you just add your custom header as a header of the tableview rather than it being the header of the first section in the tableview. This doesn't solve your specific problem, but it seems to be a better solution for your app considering there will on be only 1 section in the tableview.

In -viewDidLoad you can simply set the tableHeaderView property to your custom view as follows:

- (void)viewDidLoad{

  //...

  _tableview.tableHeaderView = [self myTableHeader];

}

- (UIView *)myTableHeader{

  //Return custom header view 

}
Michael
  • 6,561
  • 5
  • 38
  • 55