Created
November 3, 2018 09:08
-
-
Save laevandus/46d07827202329232ae6f514e644205e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class FormViewController: UITableViewController { | |
// MARK: Creating a Form View | |
let form: Form | |
init(form: Form) { | |
self.form = form | |
super.init(style: .grouped) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
// MARK: Managing the View | |
private enum ReuseIdentifiers: String { | |
case textInput | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tableView.rowHeight = 44 | |
tableView.register(TextInputTableViewCell.self, forCellReuseIdentifier: ReuseIdentifiers.textInput.rawValue) | |
} | |
// MARK: Providing Table View Content | |
private func model(at indexPath: IndexPath) -> FormItem { | |
return form.sections[indexPath.section].items[indexPath.item] | |
} | |
override func numberOfSections(in tableView: UITableView) -> Int { | |
return form.sections.count | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return form.sections[section].items.count | |
} | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let object = model(at: indexPath) | |
if let textRow = object as? TextInputFormItem { | |
let cell = tableView.dequeueReusableCell(withIdentifier: ReuseIdentifiers.textInput.rawValue, for: indexPath) as! TextInputTableViewCell | |
cell.configure(for: textRow) | |
return cell | |
} | |
else { | |
fatalError("Unknown model \(object).") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment