Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  2.  
  3. let cell = self.tableView.dequeueReusableCell(withIdentifier: "HourlyWaterCell", for: indexPath) as! HourlyWaterCell
  4. let dictionary = dataSource[indexPath.row]
  5.  
  6. if let startTime = dictionary["startTime"] as! Date? {
  7. cell.timeLabel.text = dateFormatter.string(from: startTime)
  8. }
  9.  
  10. return cell
  11. }
  12.  
  13. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  14.  
  15. tableView.deselectRow(at: indexPath, animated: true)
  16. var dictionary = dataSource[indexPath.row]
  17.  
  18. let buttonState = (tableView.cellForRow(at: indexPath) as! HourlyWaterCell).button.completed
  19. if buttonState == true {
  20. (tableView.cellForRow(at: indexPath) as! HourlyWaterCell).button.completed = false
  21. cellStates[indexPath.row] = false
  22. dictionary["selected"] = false
  23. }
  24. else {
  25. (tableView.cellForRow(at: indexPath) as! HourlyWaterCell).button.completed = true
  26. //cellStates[indexPath.row] = true
  27. dictionary["selected"] = true
  28.  
  29. graphView.fill(column: 1)
  30. }
  31.  
  32. }
  33.  
  34. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  35.  
  36. let dictionary = self.dataSource[indexPath.row]
  37. if let selectionState = dictionary["selected"] as! Bool? {
  38. if selectionState == true{
  39. //cell.button.completed = true
  40. cell.setSelected(true, animated: true)
  41. } else {
  42. //cell.button.completed = false
  43. cell.setSelected(false, animated: true)
  44. }
  45. }
  46. }
  47.  
  48. import UIKit
  49.  
  50. @IBDesignable
  51. class WaterButton: UIButton {
  52.  
  53. @IBInspectable var fillColor: UIColor = .green
  54. @IBInspectable var greyColor: UIColor = .gray
  55. @IBInspectable var completed = false
  56. let lineWidth: CGFloat = 2.0
  57. let π = CGFloat(M_PI) //option P for π
  58.  
  59. override func draw(_ rect: CGRect) {
  60. if completed {
  61. drawStrokedCircle(percentRadius:0.5, color: fillColor, filled: true)
  62. drawStrokedCircle(percentRadius:0.8, color: fillColor, filled: false)
  63. } else {
  64. drawStrokedCircle(percentRadius:0.5, color: greyColor, filled: false)
  65. }
  66. }
  67.  
  68. func drawStrokedCircle(percentRadius: CGFloat, color: UIColor, filled: Bool) {
  69. // 1
  70. let center = CGPoint(x:bounds.width/2, y: bounds.height/2)
  71.  
  72. // 2
  73. let radius: CGFloat = ((max(bounds.width, bounds.height) / 2.0 ) * percentRadius)
  74.  
  75. // 3
  76. //let arcWidth: CGFloat = 76
  77.  
  78. // 4
  79. let startAngle: CGFloat = 0
  80. let endAngle: CGFloat = 2 * π
  81.  
  82. // 5
  83. let path = UIBezierPath(arcCenter: center,
  84. radius: radius,
  85. startAngle: startAngle,
  86. endAngle: endAngle,
  87. clockwise: true)
  88.  
  89. // 6
  90. path.lineWidth = 2.0
  91. color.setStroke()
  92. path.stroke()
  93.  
  94. if filled {
  95. fillColor.setFill()
  96. path.fill()
  97. }
  98. }
  99. }
  100.  
  101. @IBInspectable var completed = false {
  102. didSet {
  103. if completed == true {
  104. drawStrokedCircle(percentRadius:0.5, color: fillColor, filled: true)
  105. drawStrokedCircle(percentRadius:0.8, color: fillColor, filled: false)
  106. } else {
  107. drawStrokedCircle(percentRadius:0.5, color: greyColor, filled: false)
  108. }
  109. }
  110. }
  111.  
  112. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  113. let cell = self.tableView.dequeueReusableCell(withIdentifier: "HourlyWaterCell", for: indexPath) as! HourlyWaterCell
  114. let dictionary = dataSource[indexPath.row]
  115. if let completed = cellButtonStates?[indexPath.row] {
  116. cell.button.completed = completed
  117. }
  118. }
  119.  
  120. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  121. /* you can perform extra evaluation to avoid
  122. iterating to cellButtonStates if not needed */
  123. for (i,_) in self.cellButtonStates.keys.enumerated() {
  124. self.cellButtonStates[i] = false
  125. }
  126. self.cellButtonStates[indexPath.row] = true
  127. }
  128.  
  129. override var isSelected: Bool {
  130. didSet {
  131. button.completed = isSelected;
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement