Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ReusableTableViewController.swift
- // LoginApp
- //
- // Created by Matheus Oliveira Costa on 05/10/19.
- // Copyright © 2019 mathocosta. All rights reserved.
- //
- import UIKit
- fileprivate enum UserProfileSection: Int, CaseIterable {
- case personal
- case sizing
- var title: String {
- switch self {
- case .personal:
- return "Personal info"
- case .sizing:
- return "Sizing info"
- }
- }
- var rows: [UserProfileRow] {
- switch self {
- case .personal:
- return [.firstName, .lastName]
- case .sizing:
- return [.height, .weight]
- }
- }
- static var allRows: [UserProfileRow] {
- return UserProfileSection.allCases.flatMap { $0.rows }
- }
- static func tag(for row: UserProfileRow) -> Int {
- return allRows.firstIndex(of: row)!
- }
- static func row(for tag: Int) -> UserProfileRow {
- return allRows[tag]
- }
- }
- fileprivate enum UserProfileRow: String {
- case firstName = "First Name"
- case lastName = "Last Name"
- case height = "Height"
- case weight = "Weight (lbs)"
- func isValid(for value: String?) -> Bool {
- // make some validation
- return true
- }
- }
- class UserProfileCache {
- var firstName: String?
- var lastName: String?
- var height: String?
- var weight: String?
- private func value(for row: UserProfileRow) -> String? {
- switch row {
- case .firstName: return firstName
- case .lastName: return lastName
- case .height: return height
- case .weight: return weight
- }
- }
- private func set(value: String?, for row: UserProfileRow) {
- switch row {
- case .firstName: firstName = value
- case .lastName: lastName = value
- case .height: height = value
- case .weight: weight = value
- }
- }
- fileprivate subscript(row: UserProfileRow) -> String? {
- get { return value(for: row) }
- set(newValue) { set(value: newValue, for: row) }
- }
- }
- protocol Reusable {}
- extension UITableViewCell: Reusable {}
- extension Reusable where Self: UITableViewCell {
- static var reuseID: String {
- return String(describing: self)
- }
- }
- extension UITableView {
- func registerCell<Cell: UITableViewCell>(_ cellClass: Cell.Type) {
- register(cellClass, forCellReuseIdentifier: cellClass.reuseID)
- }
- func dequeueReusableCell<Cell: UITableViewCell>(forIndexPath indexPath: IndexPath) -> Cell {
- guard let cell = self.dequeueReusableCell(
- withIdentifier: Cell.reuseID, for: indexPath
- ) as? Cell else { fatalError("Fatal error for cell at \(indexPath)") }
- return cell
- }
- }
- class ReusableTableViewCell: UITableViewCell {
- let textField: UITextField = {
- return UITextField()
- }()
- }
- class ReusableTableViewController: UITableViewController, UITextFieldDelegate {
- var user = UserProfileCache()
- override func viewDidLoad() {
- super.viewDidLoad()
- tableView.registerCell(ReusableTableViewCell.self)
- }
- // MARK: - Table view data source
- override func numberOfSections(in tableView: UITableView) -> Int {
- return UserProfileSection.allCases.count
- }
- override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
- guard let section = UserProfileSection(rawValue: section) else { return nil }
- return section.title
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- let section = UserProfileSection(rawValue: section)!
- return section.rows.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as ReusableTableViewCell
- let section = UserProfileSection(rawValue: indexPath.section)!
- let row = section.rows[indexPath.row]
- let value = user[row]
- cell.textLabel?.textColor = row.isValid(for: value) ? .black : .red
- let tag = UserProfileSection.tag(for: row)
- cell.textLabel?.text = row.rawValue
- cell.textField.tag = tag
- cell.textField.text = value
- cell.textField.delegate = self
- return cell
- }
- // MARK: - Text field delegate
- func textFieldShouldReturn(_ textField: UITextField) -> Bool {
- textField.resignFirstResponder()
- let row = UserProfileSection.row(for: textField.tag)
- user[row] = textField.text
- return false
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement