i have a table with a variable number of sections. If there are more than one section i want to show show custom Headers, but if there is only one, i don't want a header at all. My Approach is to use func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
and return nil
if i have only one section. But instead of showing no header, i get an empty header. Any ideas?
here is my code
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Orders"
tableView.dataSource = self
tableView.delegate = self
let headerNib = UINib.init(nibName: "SectionHeader", bundle: Bundle.main)
tableView.register(headerNib, forHeaderFooterViewReuseIdentifier: "SectionHeader")
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if(ordersList.count < 2) {return nil}
let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "SectionHeader") as! SectionHeader
headerView.label.text = "\(sectionHeaders[section]) (\(ordersList[section].count))"
headerView.button.setTitle(self.collapsed[section] ? "▶" : "▼", for: .normal)
headerView.onTap = {
self.collapsed[section] = !self.collapsed[section]
headerView.button.rotate(self.collapsed[section] ? CGFloat(-.pi/2.0) : CGFloat(.pi/2.0), duration: 0.1, completion: {
self.tableView.beginUpdates()
self.tableView.reloadSections([section], with: .fade)
self.tableView.endUpdates()
})
}
return headerView
}
use heightForHeaderInSection delegate method also. Make height 0 if condition not satisfy. For Reference -
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if(ordersList.count < 2) {return 0.1}
else { return HEIGHT} //custom value of view height
}
You have to use the following methods to achieve the desired result:
func numberOfSections(in tableView: UITableView) -> Int {
if(ordersList.count < 2) {return nil}
return ordersList.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if(ordersList.count < 2) {return nil}
//else return some view }
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if(ordersList.count < 2) {return 0}
return some_height
}
Use tableView.numberOfSections
to get the section count . And use this value in tableView(_:viewForHeaderInSection:)
to check whether you want return SectionHeader
or nil
. You need to return nil
in case you don't want to show header
in a particular section
.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard tableView.numberOfSections > 1 else {
return nil
}
let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "SectionHeader") as! SectionHeader
//add rest of the code...
return headerView
}
Implement tableView(_:heightForHeaderInSection:)
to return the header height
based on the section
. Return 0
if you don't want to show header
in a section
otherwise it will take the default header height
.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return (tableView.numberOfSections > 1) ? 100.0 : 0.0
}
you should override numberOfSections method of tableview. And you should not use dequeueReusableHeaderFooterView for section header view. Section header views should be a custom view.
-- numberOfSections
func numberOfSections(in tableView: UITableView) -> Int {
return sectionCount
}
-- sectionHeaderView
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// you can store data on a view model
let cellViewModel = viewModel.returnViewModel(index: section)
switch cellViewModel.type {
case .sampleViewModel1:
return UIView() // sampleView1
case .sampleViewModel2:
return UIView() // sampleView2
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.