mvm_core_ui/MVMCoreUI/BaseControllers/ProgrammaticTableViewController.swift
Pfeil, Scott Robert 8f4e7f82c3 update cells
2020-04-22 15:10:41 -04:00

92 lines
3.1 KiB
Swift

//
// ProgrammaticTableViewController.swift
// MVMCoreUI
//
// Created by Scott Pfeil on 3/12/20.
// Copyright © 2020 Verizon Wireless. All rights reserved.
//
import Foundation
open class ProgrammaticTableViewController: ProgrammaticScrollViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet public var tableView: UITableView!
public init(with tableView: UITableView) {
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)
}
open override func loadView() {
let view = UIView()
view.backgroundColor = .white
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()
}
/// This class should create the table view that will be used here. Subclass this for different table styles.
open func createTableView() -> UITableView {
let tableView = UITableView(frame: .zero, style: .grouped)
tableView.backgroundColor = .clear
tableView.separatorStyle = UITableViewCell.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() {
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 {
return 1
}
open func numberOfSections(in tableView: UITableView) -> Int {
return tableView.bounds.width > 1 ? getNumberOfSections() : 0
}
open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
}