As the design of table views using the grouped style changed considerably with iOS 7, I would like to hide (or remove) the first section header. So far I haven\'t managed to
this way is OK.
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return CGFloat.min
}
return 25
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
return nil
}else {
...
}
}
Update[9/19/17]: Old answer doesn't work for me anymore in iOS 11. Thanks Apple. The following did:
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 20.0f;
self.tableView.contentInset = UIEdgeInsetsMake(-18.0, 0.0f, 0.0f, 0.0);
Previous Answer:
As posted in the comments by Chris Ostomo the following worked for me:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return CGFLOAT_MIN; // to get rid of empty section header
}
I just copied your code and tried. It runs normally (tried in simulator). I attached result view. You want such view, right? Or I misunderstood your problem?
In Swift 4.2 and many earlier versions, instead of setting the first header's height to 0 like in the other answers, you can just set the other headers to nil
. Say you have two sections and only want the second one (i.e., 1
) to have a header. That header will have the text Foobar:
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return section == 1 ? "Foobar" : nil
}
Swift3 : heightForHeaderInSection works with 0, you just have to make sure header is set to clipsToBounds.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0
}
if you don't set clipsToBounds hidden header will be visible when scrolling.
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let header = view as? UITableViewHeaderFooterView else { return }
header.clipsToBounds = true
}
This is how to hide the first section header in UITableView (grouped style).
Swift 3.0 & Xcode 8.0 Solution
The TableView's delegate should implement the heightForHeaderInSection method
Within the heightForHeaderInSection method, return the least positive number. (not zero!)
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
let headerHeight: CGFloat
switch section {
case 0:
// hide the header
headerHeight = CGFloat.leastNonzeroMagnitude
default:
headerHeight = 21
}
return headerHeight
}