Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. import UIKit
  2.  
  3. class TimersViewController: UIViewController {
  4.  
  5. @IBOutlet weak var tableView: BaseTableView!
  6.  
  7. var timerArr = [Int]()
  8.  
  9. let cellId = "cellId"
  10.  
  11. override func viewDidLoad() {
  12. super.viewDidLoad()
  13.  
  14. view.backgroundColor = .blue
  15.  
  16. setupTableView()
  17.  
  18. setupNavigationBar()
  19. }
  20.  
  21. fileprivate func setupNavigationBar() {
  22. let add = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(onAddClicked))
  23.  
  24. navigationItem.rightBarButtonItems = [add]
  25. }
  26.  
  27. @objc
  28. fileprivate func onAddClicked() {
  29. self.timerArr.append(100)
  30.  
  31. tableView.reloadAsync()
  32. }
  33.  
  34. fileprivate func setupTableView() {
  35. tableView.backgroundColor = UIColor.red.withAlphaComponent(0.3)
  36.  
  37. tableView.delegate = self
  38. tableView.dataSource = self
  39.  
  40. tableView.register(CustomCell.self, forCellReuseIdentifier: cellId)
  41. }
  42. }
  43.  
  44. extension TimersViewController: UITableViewDelegate,UITableViewDataSource {
  45.  
  46. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  47. return self.timerArr.count
  48. }
  49.  
  50. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  51. let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! CustomCell
  52.  
  53. cell.configure(with: self.timerArr[indexPath.row])
  54.  
  55. cell.handler = {[weak self] (counter) in
  56. print(indexPath.row , counter)
  57.  
  58. self?.timerArr[indexPath.row] = counter
  59. }
  60.  
  61. return cell
  62. }
  63. }
  64.  
  65.  
  66. class CustomCell: UITableViewCell {
  67. //MARK: Outlets
  68. @IBOutlet weak var label: UILabel!
  69.  
  70. //MARK: Internal Properties
  71. var handler: ((Int)->())?
  72.  
  73. //MARK: Private Properties
  74. private var timer: Timer?
  75. private var counter = 100 {
  76. didSet {
  77. DispatchQueue.main.async {
  78. self.lblTitle.text = "\(self.counter)"
  79.  
  80. self.handler?(self.counter)
  81. }
  82. }
  83. }
  84.  
  85. fileprivate var lblTitle: UILabel = {
  86. let lbl = UILabel()
  87. lbl.font = UIFont.systemFont(ofSize: 50)
  88.  
  89. return lbl
  90. }()
  91.  
  92. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  93. super.init(style: style, reuseIdentifier: reuseIdentifier)
  94.  
  95. commonInit()
  96. }
  97.  
  98. required init?(coder aDecoder: NSCoder) {
  99. super.init(coder: aDecoder)
  100.  
  101. commonInit()
  102. }
  103.  
  104. fileprivate func commonInit() {
  105. self.setupView()
  106. }
  107.  
  108. //MARK: Internal Methods
  109. func configure(with counter: Int) {
  110.  
  111. self.lblTitle.text = "\(counter)"
  112.  
  113. self.counter = counter
  114.  
  115. self.setTimer()
  116. }
  117.  
  118. //MARK: Private Methods
  119. private func setTimer() {
  120. self.timer?.invalidate()
  121. self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: {[weak self] (timer) in
  122. if let counter = self?.counter, counter > 0 {
  123. self?.counter -= 1
  124.  
  125. } else {
  126. timer.invalidate()
  127. }
  128. })
  129. }
  130.  
  131. fileprivate func setupView() {
  132. //This sets the label equal to superview with padding
  133. lblTitle.safeArea(equalTo: self, padding: 10)
  134. }
  135. }
  136.  
  137. class BaseTableView: UITableView {
  138. override init(frame: CGRect, style: UITableView.Style) {
  139. super.init(frame: frame, style: style)
  140.  
  141. commonInit()
  142. }
  143.  
  144. required init?(coder aDecoder: NSCoder) {
  145. super.init(coder: aDecoder)
  146. commonInit()
  147. }
  148.  
  149. fileprivate func commonInit() {
  150. tableFooterView = UIView()
  151. }
  152. }
  153.  
  154.  
  155. extension UITableView {
  156. func reloadAsync() {
  157. DispatchQueue.main.async {
  158. self.reloadData()
  159. }
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement