Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = self.tableView.dequeueReusableCell(withIdentifier: "HourlyWaterCell", for: indexPath) as! HourlyWaterCell
- let dictionary = dataSource[indexPath.row]
- if let startTime = dictionary["startTime"] as! Date? {
- cell.timeLabel.text = dateFormatter.string(from: startTime)
- }
- return cell
- }
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- tableView.deselectRow(at: indexPath, animated: true)
- var dictionary = dataSource[indexPath.row]
- let buttonState = (tableView.cellForRow(at: indexPath) as! HourlyWaterCell).button.completed
- if buttonState == true {
- (tableView.cellForRow(at: indexPath) as! HourlyWaterCell).button.completed = false
- cellStates[indexPath.row] = false
- dictionary["selected"] = false
- }
- else {
- (tableView.cellForRow(at: indexPath) as! HourlyWaterCell).button.completed = true
- //cellStates[indexPath.row] = true
- dictionary["selected"] = true
- graphView.fill(column: 1)
- }
- }
- func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
- let dictionary = self.dataSource[indexPath.row]
- if let selectionState = dictionary["selected"] as! Bool? {
- if selectionState == true{
- //cell.button.completed = true
- cell.setSelected(true, animated: true)
- } else {
- //cell.button.completed = false
- cell.setSelected(false, animated: true)
- }
- }
- }
- import UIKit
- @IBDesignable
- class WaterButton: UIButton {
- @IBInspectable var fillColor: UIColor = .green
- @IBInspectable var greyColor: UIColor = .gray
- @IBInspectable var completed = false
- let lineWidth: CGFloat = 2.0
- let π = CGFloat(M_PI) //option P for π
- override func draw(_ rect: CGRect) {
- if completed {
- drawStrokedCircle(percentRadius:0.5, color: fillColor, filled: true)
- drawStrokedCircle(percentRadius:0.8, color: fillColor, filled: false)
- } else {
- drawStrokedCircle(percentRadius:0.5, color: greyColor, filled: false)
- }
- }
- func drawStrokedCircle(percentRadius: CGFloat, color: UIColor, filled: Bool) {
- // 1
- let center = CGPoint(x:bounds.width/2, y: bounds.height/2)
- // 2
- let radius: CGFloat = ((max(bounds.width, bounds.height) / 2.0 ) * percentRadius)
- // 3
- //let arcWidth: CGFloat = 76
- // 4
- let startAngle: CGFloat = 0
- let endAngle: CGFloat = 2 * π
- // 5
- let path = UIBezierPath(arcCenter: center,
- radius: radius,
- startAngle: startAngle,
- endAngle: endAngle,
- clockwise: true)
- // 6
- path.lineWidth = 2.0
- color.setStroke()
- path.stroke()
- if filled {
- fillColor.setFill()
- path.fill()
- }
- }
- }
- @IBInspectable var completed = false {
- didSet {
- if completed == true {
- drawStrokedCircle(percentRadius:0.5, color: fillColor, filled: true)
- drawStrokedCircle(percentRadius:0.8, color: fillColor, filled: false)
- } else {
- drawStrokedCircle(percentRadius:0.5, color: greyColor, filled: false)
- }
- }
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = self.tableView.dequeueReusableCell(withIdentifier: "HourlyWaterCell", for: indexPath) as! HourlyWaterCell
- let dictionary = dataSource[indexPath.row]
- if let completed = cellButtonStates?[indexPath.row] {
- cell.button.completed = completed
- }
- }
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- /* you can perform extra evaluation to avoid
- iterating to cellButtonStates if not needed */
- for (i,_) in self.cellButtonStates.keys.enumerated() {
- self.cellButtonStates[i] = false
- }
- self.cellButtonStates[indexPath.row] = true
- }
- override var isSelected: Bool {
- didSet {
- button.completed = isSelected;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement