mvm_core_ui/MVMCoreUI/BaseControllers/ProgrammaticTableViewController.swift
2021-08-02 17:16:11 -04:00

108 lines
3.9 KiB
Swift

//
// ProgrammaticTableViewController.swift
// MVMCoreUI
//
// Created by Scott Pfeil on 3/12/20.
// Copyright © 2020 Verizon Wireless. All rights reserved.
//
open class ProgrammaticTableViewController: ProgrammaticScrollViewController, UITableViewDelegate, UITableViewDataSource {
//--------------------------------------------------
// MARK: - Outlet
//--------------------------------------------------
@IBOutlet public var tableView: TableView!
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
public init(with tableView: TableView) {
self.tableView = tableView
super.init(with: tableView)
}
required public init?(coder: NSCoder) {
super.init(coder: coder)
}
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
open override func loadView() {
let view = UIView()
view.backgroundColor = .mvmWhite
let tableView = createTableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)
// Sets the constraints for the scroll view
let constraints = NSLayoutConstraint.constraintPinSubview(toSuperview: tableView)
topConstraint = constraints?[ConstraintTop] as? NSLayoutConstraint
bottomConstraint = constraints?[ConstraintBot] as? NSLayoutConstraint
self.tableView = tableView
scrollView = tableView
self.view = view
}
open override func viewDidLoad() {
super.viewDidLoad()
registerWithTable()
}
//--------------------------------------------------
// MARK: - Table View
//--------------------------------------------------
/// This class should create the table view that will be used here. Subclass this for different table styles.
open func createTableView() -> TableView {
let tableView = TableView(frame: .zero, style: .grouped)
tableView.backgroundColor = .clear
tableView.separatorStyle = .none
tableView.delegate = self
tableView.dataSource = self
tableView.insetsContentViewsToSafeArea = false
return tableView
}
/// Registers classes and nibs. Can subclass for different nibs. Can call super and then add new ones after as well.
open func registerWithTable() { }
/// Sets the table to have no section headers or footers.
open func setNoSectionHeadersFooters() {
tableView.sectionHeaderHeight = CGFloat.leastNormalMagnitude
tableView.sectionFooterHeight = CGFloat.leastNormalMagnitude
}
open func refreshTable() {
// Perform asyncronously to ensure it can never run in the same process as table data updates.
DispatchQueue.main.async { [self] in
tableView.beginUpdates()
tableView.endUpdates()
}
}
/// For subclassing, returns the number of sections for table. This function calls numberOfSectionsForTableview aftre ensuring the table is setup properly.
open func getNumberOfSections() -> Int { 1 }
open func numberOfSections(in tableView: UITableView) -> Int {
tableView.bounds.width > 1 ? getNumberOfSections() : 0
}
open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 0 }
open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { UITableViewCell() }
open func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { }
open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { }
}