Skip to content

Instantly share code, notes, and snippets.

@bill350
Last active January 18, 2018 20:36
Show Gist options
  • Save bill350/e5b931b30a8d12f410a8b30faaaf3aa6 to your computer and use it in GitHub Desktop.
Save bill350/e5b931b30a8d12f410a8b30faaaf3aa6 to your computer and use it in GitHub Desktop.
import UIKit
class FormViewController: UIViewController {
@IBOutlet weak var ibTableView: UITableView!
fileprivate var form = Form()
// MARK: View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
self.form = Form()
self.title = self.form.title
self.prepareSubViews()
self.ibTableView.reloadData()
}
private func prepareSubViews() {
//Prepare tableView
FormItemCellType.registerCells(for: self.ibTableView)
self.ibTableView.allowsSelection = false
self.ibTableView.estimatedRowHeight = 60
self.ibTableView.rowHeight = UITableViewAutomaticDimension
}
@IBAction func didTapValidate(_ sender: Any) {
self.form.isValid()
self.ibTableView.reloadData()
}
}
// MARK: - UITableViewDataSource
extension FormViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.form.formItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = self.form.formItems[indexPath.row]
let cell: UITableViewCell
if let cellType = self.form.formItems[indexPath.row].uiProperties.cellType {
cell = cellType.dequeueCell(for: tableView, at: indexPath)
} else {
cell = UITableViewCell() //or anything you want
}
if let formUpdatableCell = cell as? FormUpdatable {
item.indexPath = indexPath
formUpdatableCell.update(with: item)
}
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment