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 ?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With