Home>
Thing I want to do

I want to add a custom header view to a tableView created with xib.

Premise

Xcode: 12.1
Swift: 5.3.1

What i did

❶ Create view with xib (HeaderView.xib)
❷ Create a class that inherits UITableViewHeaderFooterView. In the control class of ❶. (Header.swift)
❸ Generate section View as original header with tableView.
・ The code is below.
❹ When the style of tableView is set to "plain".
・ It follows even if the header scrolls
❺ When the style of tableView is set to "Grouped".
-The header disappears.

I want to prevent the added header from following when scrolling.
I would appreciate it if anyone could give me some advice.
Thank you.

 override func viewDidLoad () {
        super.viewDidLoad ()
        tableView.delegate = self
        tableView.dataSource = self
        // read the xib file
        let nib = UINib (nibName: "TableViewCell", bundle: nil)
        tableView.register (nib, forCellReuseIdentifier: "reuseIdentifier")
        tableView.separatorStyle = .none
        tableView.allowsSelection = false
        // Add header
        let headerNib = UINib (nibName: "HeaderView", bundle: nil)
        tableView.register (headerNib, forHeaderFooterViewReuseIdentifier: "HeaderView")
        // Determine the height of the section header
        tableView.sectionHeaderHeight = 200
    }
    // headerView
    override func tableView (_ tableView: UITableView, viewForHeaderInSection section: Int)->UIView? {
        let view = tableView.dequeueReusableHeaderFooterView (withIdentifier: "HeaderView")
        return view
    }