5

In a view controller I have only a UITableView. In IB I have made Header & Footer height as 1, and have also added the following code but yet above the 1st cell their is lots of space of header. I want to get rid of that space. Even the scrollbar starts from the 1st cell and not on top.

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
CGFloat height = 0.0001;
return height;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
CGFloat height = 0.0001;
return height;
}

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// Create Empty View
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width,
                                                        [self tableView:self.visitorlistsTv heightForHeaderInSection:section]) ];
return view;
}

-(UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
// Create Empty View
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width,
                                                        [self tableView:self.visitorlistsTv heightForFooterInSection:section]) ];
return view;

}

With the above code, the footer part is hidden. But can't get the header hidden too.

Looking at other links for solution I have also added TableView in a View, have added constraints to the tableview, but still the header part is still their.

Where am I going wrong ? How to get rid of it ?

JackWhiteIII
  • 1,388
  • 2
  • 11
  • 25
Tvd
  • 4,463
  • 18
  • 79
  • 125
  • Why do you need a header & footer with such a small height? This looks like a pretty bad hack. What problem do you want to solve? – Rinat Khanov Aug 05 '14 at 16:04
  • I read that returning 0 in height doesn't make any difference, hence I tried returning such small header. On top of table above first cell, their is empty space, I want to remove that space. – Tvd Aug 05 '14 at 16:07

6 Answers6

6

If you're trying to remove the space above your UITableView, try changing it's contentInset property instead of making a custom header with small height:

self.tableView.contentInset = UIEdgeInsetsMake(-20.0f, 0.0f, 0.0f, 0.0f);

You may want to tweak the first number to adjust it to your specific view scene.

Understand that this is also a workaround (source), but it's more clean method than creating a custom table view header.

Community
  • 1
  • 1
Rinat Khanov
  • 1,566
  • 10
  • 32
  • Thanks that worked. I had added that line, I changed 1st para to -20 and then could see the changes. I had to go till -60 to remove it completely. Any idea why and what is that space for ? – Tvd Aug 05 '14 at 16:15
  • My table is not grouped also - Style in IB is Plain and not Grouped. – Tvd Aug 05 '14 at 16:17
1

I have another solution for this (checked on iOS 10), for me the accepted solution didn't work.

In my case, I had variable height section headers, so returning 0 height for first section was not acceptable. I used grouped table style because I needed the separators to frame the section headers too.

Solution: be sure you don't have something like this anywhere in your code:

self.table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

I used this to remove the separators for the empty rows, but with grouped style that is not necessary.

I know this won't be the case for all of you, but it might help for those in my situation.

Calin Drule
  • 2,899
  • 1
  • 15
  • 12
0

It seems to me the problem here is that you don't want a header/footer at all in your view. If this is the case, you don't want to return any view in the header/footer. I would recommend either returning nil in the viewForHeader/Footer or not implementing those functions altogether - they are optional.

Mike
  • 9,765
  • 5
  • 34
  • 59
  • Yes I don't want Header/Footer totally. But not implementing or returning 0 and nil in height/viewFor methods made no difference in header. With Footer it works. For Header its only setting contentInset worked. Rest nothing worked for header part. – Tvd Aug 05 '14 at 16:49
  • I would try removing those altogether from the code, and perhaps try to fix in in IB? – Mike Aug 05 '14 at 17:05
  • I totally agree with you. I tried that way too. After spending so much time on it, I asked here. – Tvd Aug 05 '14 at 17:18
0

With Navigationbar you can change the Top values

self.tableView.contentInset =UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)

self.tableView.contentInset = UIEdgeInsetsMake(-75.0f, 0.0f, 0.0f, 0.0f);

Atef
  • 2,872
  • 1
  • 36
  • 32
0

If you want to avoid setting hard coded height, you can just set the contentOffset top from your first cell Y position like this:

- (void)viewDidLayoutSubviews
{
  UITableViewCell *firstCell = (self.tableView.visibleCells.count > 0?self.tableView.visibleCells[0]:nil);

  [super viewDidLayoutSubviews];

  // Get rid of the defaut header height.
  self.tableView.contentInset = UIEdgeInsetsMake(-firstCell.frame.origin.y, 0, 0, 0);
}
Phil
  • 2,784
  • 2
  • 28
  • 26
-1

Use this code for removing space between first cell of uitableview.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
 {
    return 0.002f;// set this...
 }
Shahzaib Maqbool
  • 1,479
  • 16
  • 25