2017-03-23 4 views
1

TableView의 셀에 올바른 선택 상태가 표시되는 올바른 절차는 무엇입니까? 아래 표에는 UIButton의 하위 클래스로 만든 이러한 사용자 지정 지표가 있습니다. didSelectRowAtIndexPath가 두드리면 점등과 점등 사이를 토글합니다. tableView의 dataSource는 사전의 배열이며 사전은 각 셀의 상태와 관련된 정보를 보유합니다.TableViewCells를 올바르게 업데이트하는 방법

내가 해결하려고하는 과제는 목록을 스크롤하면 잘못된 셀이 켜지거나 꺼지는 것이고 셀 재사용의 기능이라고 생각됩니다.

enter image description here

나는 willDisplayCellForRowAtIndexPath에 코드를 넣어 시도했습니다. 전에이 문제를 해결 한 것 같지만이 문제에 붙어 있습니다. 여기

사용자 정의 버튼 클래스

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() 
     } 
    } 
} 

답변

0
나는 정확히 같은 일을했다

있는 tableView 방법 여기

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) 
     } 
    } 
} 

하고 있지만, 그것은 얼마 전에 내가 액세스 할 수 없습니다 더 이상 코드에.

그러나 방법으로 선택을 설정하지 않았습니다. 대신 cellForRowAt에 선택 상태를 설정했습니다.

또한 셀이 테이블보기 셀 내부에서 선택되면 부기를 구현해야 할 수도 있습니다. 희망이 도움이!

는 :

0

그런 다음 위임에 WaterButton의 선택 상태 (completed)를 저장할 때마다 버튼의 상태 당신의 버튼 (cellForRowAtIndexPath를) dequed되어 보유하고있는 셀을 복원 할 수 있습니다. WaterButton에서 '완성 된'속성의 가치 변화를 관찰하십시오.

@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) 
     } 
    } 
} 

복원 당신의 tableview의 위임에 바르를 추가

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 
    //call reloadData 
} 
:

var cellButtonStates : [NSInteger : Bool]?

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 
    } 
} 

은 분명히 당신이 사용자가 셀을 선택하는 cellButtonStates 매번 업데이트해야

당신은 sti에 원한다면 (에 isSelected 사용) 코드와 CK, 나는 아래처럼 HourlyWaterCell에에 isSelected의 속성을 재정의 제안 : 코드의

class HourlyWaterCell: UITableViewCell { 
override var isSelected: Bool { 
    didSet { 
     button.completed = isSelected; 
    } 
} 

없음 위의 테스트를하지 않습니다.

+0

제안 해 주셔서 감사합니다. 불행히도 그것은 작동하지 않습니다. – Aaronium112