Bug scenario
From the head office came all the TableViews. All the sectionHeader and sectionFooter came from the Grouped tableView. So initialize frame to.zero and update the header height when the data request comes back.
The default y value of the sectionHeader for group 0 is 35, so you can see the background color of the tableView.
Code and renderings
code
-
The code is relatively simple, just a common list, part of the writing is just to meet this example, different from normal development, so please understand, the cell click event simulation data after the return of the update header height.
import UIKit fileprivate let UITableViewCellID = "UITableViewCellID" class ViewController: UIViewController { let size = UIScreen.main.bounds.size private lazy var header: UIView = { let v = UIView(frame: .zero) v.backgroundColor = .cyan return v }() private lazy var tableView: UITableView = { let fRect = CGRect(x: 0, y: 0, width: size.width, height: size.height) let tv = UITableView(frame: fRect, style: .grouped) tv.backgroundColor = .blue tv.separatorStyle = .none tv.rowHeight = 40 tv.showsVerticalScrollIndicator = false tv.tableHeaderView = header tv.delegate = self tv.dataSource = self tv.contentInsetAdjustmentBehavior = .never tv.register(UITableViewCell.self, forCellReuseIdentifier: UITableViewCellID) return tv }() override func viewDidLoad() { super.viewDidLoad() view.addSubview(tableView) } } extension ViewController: UITableViewDelegate, UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { return 4 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 3 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: UITableViewCellID, for: IndexPath) cell.textLabel?. Text = "\(indepath.section) group -- \(indepath.row) line "return cell} func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let header = UILabel(frame: .zero) header.backgroundColor = .red header.font = UIFont.systemFont(ofSize: 18); Text = "\(section) group -- header "return header} func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {return 50.0} func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { let footer = UILabel(frame: .zero) footer.backgroundColor = .green footer.font = UIFont.systemFont(ofSize: 18); Footer. Text = "\(section) group -- end "return footer} func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {return 50.0} func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { header.frame.size.height = 200; tableView .reloadData() } }Copy the code
rendering
-
Effect:
-
The offset
The solution
After searching online and trying, the final solution is to set the frame during header initialization. The determining factor is the height. Setting the absolute value of the height greater than 0.1 is ok, and testing -1 is ok. Ultimately, there are two ways.
Methods a
-
These are specific numbers, like -1, 1, 100, depending on your specific needs.
private lazy var header: UIView = { let v = UIView(frame: CGRect(x: 0, y: 0, width: size.width, height: -1)) v.backgroundColor = .cyan return v }() Copy the code
You can clearly see a line at the top, which can be set to white or transparent color in actual development.
Way 2
-
CGFloat leastNormalMagnitude corresponding OC is CGFLOAT_MIN
private lazy var header: UIView = { let v = UIView(frame: CGRect(x: 0, y: 0, width: size.width, height: -1)) v.backgroundColor = .cyan return v }() Copy the code
This scheme is perfect, there are no lines at the top.
Update header effect
-
When you click the cell, update the header height to 200. At this time, you need to refresh the list; otherwise, the display will be abnormal.
Because my model is limited, so the specific situation is treated in a specific way, and may not be appropriate in some cases, so it is only for reference. For those of you who can see this, thank you for supporting 😄😄😄