Reputation: 621
I have a grouped tableView and it has four sections , I would like to reduce the size of the header section in some section headers and I would like to remove the section header in other sections. I have tried solutions in most questions similar to mine but nothing works.
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
tableView.separatorStyle = .none
tableView.clipsToBounds = true
self.tableView.sectionHeaderHeight = UITableView.automaticDimension
self.tableView.estimatedSectionHeaderHeight = 200
self.registerTableViewCells()
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
(view as? UITableViewHeaderFooterView)?.textLabel?.textColor = UIColor.gray
if section == 0 {
(view as? UITableViewHeaderFooterView)?.contentView.backgroundColor = UIColor.white
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
switch section {
case 0:
let headerView = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "\(TestHeaderView.self)" ) as! TestHeaderView
return headerView
case 1, 2, 3 :
return nil
default:
return nil
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 0, 1, 2:
return nil
case 3:
return " some title"
default:
return nil
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
switch section {
case 0:
return UITableView.automaticDimension
case 1:
return CGFloat(10.0)
case 2:
return CGFloat(10.0)
case 3:
return CGFloat(43.0)
default:
return 0
}
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNonzeroMagnitude
}
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return 250
}
I would like to have the tableView section headers to have specific heights as highlighted in heightForHeaderInSection.
Upvotes: 0
Views: 1089
Reputation: 4210
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
}
}
Upvotes: 1
Reputation: 496
have you added UITableViewdelegate
and UITableViewDataSource
?
or try this self.tableView.estimatedSectionHeaderHeight = 80
Upvotes: 0