How to Change Height of Grouped Uitableview Header

How to change height of grouped UITableView header?

It appears that I can't set a table header view with height of 0. I ended up doing the following:

- (void)viewWillAppear:(BOOL)animated{
CGRect frame = self.tableView.tableHeaderView.frame;
frame.size.height = 1;
UIView *headerView = [[UIView alloc] initWithFrame:frame];
[self.tableView setTableHeaderView:headerView];
}

How to adjust or remove grouped tableView Header size

You don't seem to be setting the dataSource or tableView delegates (unless it is happening in some part of the code not shown). These will be needed for this (and most dynamic tableview functions).

For headers/footers that won't change size it is reasonably straightforward to create the them - return the view you want to use from tableView(_:viewForHeaderInSection:) and the height you want in tableView(_:heightForHeaderInSection:). Set these to nil and 0 respectively to hide a header.

A simple example that provides different size headers for section 0 & 2, no header for section 1, and hides all footers. I've set the headers background colours to allow them to stand out, and not provided the other methods such as cellForRowAt. Note the height is controlled by the height method, not the frame height.

class MyVC1: UIViewController {
var mapa: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
title = "Dummy TableView"
view.backgroundColor = .lightGray
let tableView = UITableView(frame: CGRect(x:50, y:100, width: 300, height: 680), style: .grouped)
view.addSubview(tableView)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.dataSource = self
tableView.delegate = self
}
}

//add dataSource and delegate support
extension MyVC1: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
5
}

func numberOfSections(in tableView: UITableView) -> Int {
3
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
switch section {
case 0:
let v = UIView(frame: CGRect(x: 10, y: 10, width: tableView.frame.width - 20, height: 90))
v.backgroundColor = .purple
return v
case 1: return nil
case 2:
let v = UIView(frame: CGRect(x: 10, y: 10, width: tableView.frame.width - 20, height: 20))
v.backgroundColor = .magenta
return v
default: fatalError()
}
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
switch section {
case 0: return CGFloat(60)
case 1: return CGFloat(0)
case 2: return CGFloat(100)
default: fatalError()
}
}

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
nil
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
0
}

}

How to change the height BETWEEN sections in GROUPED UITableView?

You could use footer views between each of your sections and give them the size of the space you desire by implementing the following methods in your UITableView's delegate.

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

In the first delegate method you can return a simple UIView instance, and in the second you can return the height that this view should be. This second delegate method should allow you to control the gap between sections.

How to change the header font of a grouped uitableview?

I try to do it. It works.

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.font = UIFont(name: "Futura", size: 15)
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UITableViewHeaderFooterView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.bounds.width, height: tableView.sectionHeaderHeight))
view.contentView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
view.contentView.backgroundColor = UIColor.init(red: 235/255, green: 235/255, blue: 235/255, alpha: 0)
view.textLabel?.text = "Hello"
return view
}

or

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
//
let label = header.viewWithTag(1000) as? UILabel
label?.font = UIFont(name: "Futura", size: 15)
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UITableViewHeaderFooterView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.bounds.width, height: tableView.sectionHeaderHeight))
view.contentView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
view.contentView.backgroundColor = UIColor.init(red: 235/255, green: 235/255, blue: 235/255, alpha: 0)
let label = UILabel.init(frame: CGRect.init(x: 20, y: 0, width: tableView.bounds.width, height: tableView.sectionHeaderHeight))
label.font = UIFont(name: "Futura", size: 11)
label.textColor = UIColor.blue
view.addSubview(label)
label.text = "Hello"
label.tag = 1000
return view
}

I hope that would be useful for you.

Header views height for grouped UITableView on iOS7

After much searching, I have finally found a fix for this. The tableview's delegate needs to implement heightForFooterInSection and return a very small number. Returning 0 defaults to the same spacing that was causing the extra spaces.

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return CGFLOAT_MIN;
}

How to hide first section header in UITableView (grouped style)

I have a workaround that seems reasonably clean to me. So I'm answering my own question.

Since 0 as the first section header's height doesn't work, I return 1. Then I use the contentInset to hide that height underneath the navigation bar.

Objective-C:

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
return 1.0f;
return 32.0f;
}

- (NSString*) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return nil;
} else {
// return some string here ...
}
}

- (void) viewDidLoad
{
[super viewDidLoad];

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

Swift:

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return section == 0 ? 1.0 : 32
}

override func viewDidLoad() {
super.viewDidLoad()

tableView.contentInset = UIEdgeInsets(top: -1, left: 0, bottom: 0, right: 0)
}


Related Topics



Leave a reply



Submit