Skip to content

Instantly share code, notes, and snippets.

@bill350
Last active January 18, 2018 20:35
Show Gist options
  • Save bill350/10c47f3433eba9d917a9057e920d6de2 to your computer and use it in GitHub Desktop.
Save bill350/10c47f3433eba9d917a9057e920d6de2 to your computer and use it in GitHub Desktop.
import Foundation
import UIKit
/// Conform receiver to have data validation behavior
protocol FormValidable {
var isValid: Bool {get set}
var isMandatory: Bool {get set}
func checkValidity()
}
/// Conform the view receiver to be updated with a form item
protocol FormUpdatable {
func update(with formItem: FormItem)
}
/// Conform receiver to have a form item property
protocol FormConformity {
var formItem: FormItem? {get set}
}
/// UI Cell Type to be displayed
enum FormItemCellType {
case textField
case textView
/// Registering methods for all forms items cell types
///
/// - Parameter tableView: TableView where apply cells registration
static func registerCells(for tableView: UITableView) {
tableView.register(cellType: FormTextFieldTableViewCell.self)
tableView.register(cellType: FormTextViewTableViewCell.self)
}
/// Correctly dequeue the UITableViewCell according to the current cell type
///
/// - Parameters:
/// - tableView: TableView where cells previously registered
/// - indexPath: indexPath where dequeue
/// - Returns: a non-nullable UITableViewCell dequeued
func dequeueCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell
switch self {
case .textView:
cell = tableView.dequeueReusableCell(for: indexPath,
cellType: FormTextViewTableViewCell.self)
case .textField:
cell = tableView.dequeueReusableCell(for: indexPath,
cellType: FormTextFieldTableViewCell.self)
}
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment