Skip to content

Instantly share code, notes, and snippets.

@shawn-frank
Last active February 8, 2023 00:45
Show Gist options
  • Save shawn-frank/c5c6504289772b99f2b6cf22f2d31d20 to your computer and use it in GitHub Desktop.
Save shawn-frank/c5c6504289772b99f2b6cf22f2d31d20 to your computer and use it in GitHub Desktop.
UITableViewHeader
//
// UITableViewHeaderSectionView.swift
// Site Visit
//
// Created by Shawn Frank on 10/06/2021.
//
import UIKit
class UITableViewHeaderSectionView: UITableViewHeaderFooterView {
private var titleLabel = UILabel()
static let identifier = "UITableViewHeaderSectionView"
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
prepareTitleLabel()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
configureLabelConstraints()
}
var text: String = "" {
didSet {
titleLabel.text = text
}
}
// MARK: UI CONFIG
private func prepareTitleLabel() {
titleLabel = UILabel()
contentView.addSubview(titleLabel)
backgroundColor = .clear
contentView.backgroundColor = .white
titleLabel.backgroundColor = .clear
titleLabel.textColor = .black
titleLabel.font = UIFont.boldSystemFont(ofSize: 20)
titleLabel.textAlignment = .left
titleLabel.text = text
}
private func configureLabelConstraints() {
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: Constants.SavedReportsCellConstants.cellLeadingTrailingConstant).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -Constants.SavedReportsCellConstants.cellLeadingTrailingConstant).isActive = true
titleLabel.heightAnchor.constraint(equalToConstant: Constants.SavedReportsCellConstants.sectionTitleHeightConstant).isActive = true
titleLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
print("Frame: \(bounds)")
layoutIfNeeded()
}
}
// MainViewController.swift
class MainViewController: UIViewVontroller {
let tableView = UITableView()
private var isLayoutConfigured = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if !isLayoutConfigured {
configureTableView()
}
}
private func configureTableView() {
view.addSubview(tableView)
tableView.register(SavedReportsTableViewCell.self,
forCellReuseIdentifier: SavedReportsTableViewCell.identifier)
tableView.register(UITableViewHeaderSectionView.self,
forHeaderFooterViewReuseIdentifier: UITableViewHeaderSectionView.identifier)
tableView.dataSource = self
tableView.delegate = self
tableView.separatorStyle = .none
tableView.tableFooterView = UIView()
tableView.translatesAutoresizingMaskIntoConstraints = false
configureTableViewAutoLayout()
}
}
extension MainViewController: UITableViewDataSource {
// remember to add the remaining UITableViewDataSource functions
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionView = tableView.dequeueReusableHeaderFooterView(withIdentifier: SavedReportTableViewSectionView.identifier) as! SavedReportTableViewSectionView
sectionView.text = "21 April 2021"
return sectionView
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return Constants.SavedReportsCellConstants.sectionHeightConstant
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment