2017-02-16 5 views
0

문제는 행의 필드 선택기 -SwiftyPickerPopover-에서 일부 값을 선택하면 작업이 모든 행에 대해 동일한 값 (선택기에서 선택된 값)을 업데이트한다는 것입니다.UITableView 셀 및 SwiftyPickerPopover 업데이트 문제

나는 (내 jQuery과 사용)의 ViewController이를 위해 사용하고 코드 : 나는 SWIFT 사용하고

protocol ExpenseDetailDelegate { 
    func switchChanged(at index: IndexPath) 
    func amountEditedChanged(at index: IndexPath) 
    func btnExpenseTypePressed(at index: IndexPath) 
    func btnCostCenterPressed(at index: IndexPath) 
} 
class ExpenseDetailCell: UITableViewCell { 
    var delegate: ExpenseDetailDelegate! 
    var indexPath: IndexPath! 

    var dataManager = DataManager.sharedInstance 

    @IBOutlet weak var txtAmountNumber: UITextField! 
    @IBOutlet weak var txtId: UITextField! 
    @IBOutlet weak var txtAmountPercent: UITextField! 
    @IBOutlet weak var lblTotal: UILabel! 
    @IBOutlet weak var lblSeparator: UILabel! 
    @IBOutlet weak var lblPercent: UILabel! 
    @IBOutlet weak var btnExpenseType: UIButton! 
    @IBOutlet weak var btnCostCenter: UIButton! 
    @IBOutlet weak var switchID: UISwitch! 

    @IBAction func btnExpenseTypePressed(_ sender: Any) { 
     debugPrint("En btnExpenseTypePressed") 
     self.delegate?.btnExpenseTypePressed(at: indexPath) 
     debugPrint("El INDEX \(indexPath)") 
    } 

    @IBAction func btnCostCenterPressed(_ sender: Any) { 
     debugPrint("En btnCostCenterPressed") 
     self.delegate?.btnCostCenterPressed(at: indexPath) 
     debugPrint("El INDEX \(indexPath)") 
    } 

    @IBAction func amountEditedChanged(_ sender: Any) { 
     debugPrint("En amountEditedChanged") 
     self.delegate?.amountEditedChanged(at: indexPath) 
     lblTotal.text = txtAmountNumber.text 
    } 

    @IBAction func switchChanged(_ sender: Any) { 
     debugPrint("En value changed") 
     self.delegate?.switchChanged(at: indexPath) 
     if switchID.isOn { 
      debugPrint("Dentro if") 
      txtId.text = "" 
      txtId.isEnabled = false 
     } else { 
      txtId.isEnabled = true 
     } 
    } 
} 

을 :

func btnExpenseTypePressed(at index: IndexPath) { 
     var tiposGasto: [String] = [String]() 
     for gasto in dataManager.expensesType { 
      tiposGasto.append(gasto.tipoGasto!) 
     } 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: index) as! ExpenseDetailCell 
     StringPickerPopover.appearFrom(
     originView: cell.btnExpenseType, 
     baseViewController: self, 
     title: "Tipos de Gasto", 
     choices: tiposGasto, 
     initialRow:0, 
     doneAction: { selectedRow, selectedString in 
     print("Seleccion \(selectedString)") 
      //self.valueSelected = selectedString 
      cell.btnExpenseType.setTitle(selectedString, for: .normal) 
      self.tableView.reloadData() 
     } ,cancelAction: { print("cancel")} 
     ) 
    } 

    func btnCostCenterPressed(at index: IndexPath) { 
     var centroCoste: [String] = [String]() 
     for centro in dataManager.costsCenter { 
      centroCoste.append(centro.centroCoste!) 
     } 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: index) as! ExpenseDetailCell 
     StringPickerPopover.appearFrom(
      originView: cell.btnCostCenter, 
      baseViewController: self, 
      title: "Centro de Coste", 
      choices: centroCoste, 
      initialRow:0, 
      doneAction: { selectedRow, selectedString in 
       print("Seleccion \(selectedString)") 
       //self.valueSelected = selectedString 
       cell.btnCostCenter.setTitle(selectedString, for: .normal) 
       self.tableView.reloadData() 
     } ,cancelAction: { print("cancel")} 
     ) 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell : UITableViewCell? = UITableViewCell() 
     if tableView == self.tableView { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) as! ExpenseDetailCell 
      cell.selectionStyle = .none 
      cell.delegate = self 

      debugPrint("First table load \(firstTableLoad)") 
      if firstTableLoad { 
       cell.btnCostCenter.setTitle(dataManager.costsCenter[0].centroCoste, for: .normal) 
       cell.btnExpenseType.setTitle(dataManager.expensesType[0].tipoGasto, for: .normal) 
       firstTableLoad = false 
      } 
      return cell 
     } 
     if tableView == self.tableViewImages { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "imagesCellIdentifier", for: indexPath) as! ExpenseImageCell 
      cell.imageView?.image = UIImage(named: "imgPlaceholder") 
      cell.selectionStyle = .none 
      return cell 
     } 
     return cell! 
} 

btnExpenseTypePressed 및 btnCostCenterPressed 방법이 ExpenseDetailCell에서 대리자 3.

답변

0

나는이 오류가 여러분이하고 싶은 일과 실제로하고있는 일에 대한 오해에서 비롯된 것이라고 생각합니다.

당신이 원하는 : 변경 한 셀

에서 하나 개의 값 당신이 할 : 세포 생성에있는 모든 세포에서 변경 한 값입니다.

당신이 호출 할 때 : self.tableView.reloadData()unc btnExpenseTypePressed(at index: IndexPath)에 아이폰 OS는 또한 창조의 모든 세포가 동일한 값을 가질 것을 의미 할 DataManager.costsCenter[0]0에 인덱스를 설정 한

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) again and again skipping the `firstTableLoad = false` and loading all the tableviewCells with the same value. 

를 호출합니다.

나의 제안 : 것은에서 변경을 수행

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{ 

} 

혹시 이것은 당신이 셀을 청소 확인에 도움이되지 않는 경우 :

override func prepareForReuse() 
{ 
    super.prepareForReuse() 
} 

하거나 이런 식으로 설정 :

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell : UITableViewCell? = UITableViewCell() 
    if tableView == self.tableView { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) as! ExpenseDetailCell 
     cell.selectionStyle = .none 
     cell.delegate = self 
     cell.setMyCustomText("")... 
     } 
     return cell 
    }