Home > Enterprise >  Swift - Mix of Custom and default TableView Headers
Swift - Mix of Custom and default TableView Headers

Time:11-02

In a tableview I'd like 1 custom header and the rest to be the default style, but I'm unsure how to specify the default header. So far I have:

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 1 {
            self.view.addSubview(viewDimensionHeader)
            return viewDimensionHeader // Works great

        } else {
            let view = UIView() // Not sure how to return default section header
            return view
        }
    }

The custom header works fine. Just looking for what to call to use the standard header.

Thanks

CodePudding user response:

Return an instance of UITableViewHeaderFooterView to get the default header/footer. You can then set background/text etc on the standard view.

Note: you really should register all header and footer views with the tableView (as you do for tableView cells):

tableView.register(viewDimensionHeader.self, forHeaderFooterViewReuseIdentifier: "CustomHeaderCell")

and then you can dequeue (reuse) them for performance gains:

let header cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "CustomHeaderCell")

Also note that you shouldn't add the header view as a subView of the main view - the tableView will take care of that for you, adding it as its own subView. Just return the cell.

  • Related