Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. //
  2. // ReusableTableViewController.swift
  3. // LoginApp
  4. //
  5. // Created by Matheus Oliveira Costa on 05/10/19.
  6. // Copyright © 2019 mathocosta. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. fileprivate enum UserProfileSection: Int, CaseIterable {
  12. case personal
  13. case sizing
  14.  
  15. var title: String {
  16. switch self {
  17. case .personal:
  18. return "Personal info"
  19. case .sizing:
  20. return "Sizing info"
  21. }
  22. }
  23.  
  24. var rows: [UserProfileRow] {
  25. switch self {
  26. case .personal:
  27. return [.firstName, .lastName]
  28. case .sizing:
  29. return [.height, .weight]
  30. }
  31. }
  32.  
  33. static var allRows: [UserProfileRow] {
  34. return UserProfileSection.allCases.flatMap { $0.rows }
  35. }
  36.  
  37. static func tag(for row: UserProfileRow) -> Int {
  38. return allRows.firstIndex(of: row)!
  39. }
  40.  
  41. static func row(for tag: Int) -> UserProfileRow {
  42. return allRows[tag]
  43. }
  44. }
  45.  
  46. fileprivate enum UserProfileRow: String {
  47. case firstName = "First Name"
  48. case lastName = "Last Name"
  49. case height = "Height"
  50. case weight = "Weight (lbs)"
  51.  
  52. func isValid(for value: String?) -> Bool {
  53. // make some validation
  54. return true
  55. }
  56. }
  57.  
  58. class UserProfileCache {
  59. var firstName: String?
  60. var lastName: String?
  61. var height: String?
  62. var weight: String?
  63.  
  64. private func value(for row: UserProfileRow) -> String? {
  65. switch row {
  66. case .firstName: return firstName
  67. case .lastName: return lastName
  68. case .height: return height
  69. case .weight: return weight
  70. }
  71. }
  72.  
  73. private func set(value: String?, for row: UserProfileRow) {
  74. switch row {
  75. case .firstName: firstName = value
  76. case .lastName: lastName = value
  77. case .height: height = value
  78. case .weight: weight = value
  79. }
  80. }
  81.  
  82. fileprivate subscript(row: UserProfileRow) -> String? {
  83. get { return value(for: row) }
  84. set(newValue) { set(value: newValue, for: row) }
  85. }
  86. }
  87.  
  88. protocol Reusable {}
  89. extension UITableViewCell: Reusable {}
  90.  
  91. extension Reusable where Self: UITableViewCell {
  92. static var reuseID: String {
  93. return String(describing: self)
  94. }
  95. }
  96.  
  97. extension UITableView {
  98. func registerCell<Cell: UITableViewCell>(_ cellClass: Cell.Type) {
  99. register(cellClass, forCellReuseIdentifier: cellClass.reuseID)
  100. }
  101.  
  102. func dequeueReusableCell<Cell: UITableViewCell>(forIndexPath indexPath: IndexPath) -> Cell {
  103. guard let cell = self.dequeueReusableCell(
  104. withIdentifier: Cell.reuseID, for: indexPath
  105. ) as? Cell else { fatalError("Fatal error for cell at \(indexPath)") }
  106.  
  107. return cell
  108. }
  109. }
  110.  
  111. class ReusableTableViewCell: UITableViewCell {
  112. let textField: UITextField = {
  113. return UITextField()
  114. }()
  115. }
  116.  
  117. class ReusableTableViewController: UITableViewController, UITextFieldDelegate {
  118.  
  119. var user = UserProfileCache()
  120.  
  121. override func viewDidLoad() {
  122. super.viewDidLoad()
  123.  
  124. tableView.registerCell(ReusableTableViewCell.self)
  125. }
  126.  
  127. // MARK: - Table view data source
  128. override func numberOfSections(in tableView: UITableView) -> Int {
  129. return UserProfileSection.allCases.count
  130. }
  131.  
  132. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  133. guard let section = UserProfileSection(rawValue: section) else { return nil }
  134. return section.title
  135. }
  136.  
  137. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  138. let section = UserProfileSection(rawValue: section)!
  139. return section.rows.count
  140. }
  141.  
  142. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  143. let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as ReusableTableViewCell
  144.  
  145. let section = UserProfileSection(rawValue: indexPath.section)!
  146. let row = section.rows[indexPath.row]
  147.  
  148. let value = user[row]
  149. cell.textLabel?.textColor = row.isValid(for: value) ? .black : .red
  150.  
  151. let tag = UserProfileSection.tag(for: row)
  152. cell.textLabel?.text = row.rawValue
  153.  
  154. cell.textField.tag = tag
  155. cell.textField.text = value
  156. cell.textField.delegate = self
  157.  
  158. return cell
  159. }
  160.  
  161. // MARK: - Text field delegate
  162. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  163. textField.resignFirstResponder()
  164. let row = UserProfileSection.row(for: textField.tag)
  165. user[row] = textField.text
  166.  
  167. return false
  168. }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement