2017-02-16 87 views
0

问题是,当我在一行中选择某个值(在字段选择器-SwiftyPickerPopover-中)时,该操作会为所有行更新相同的值(在选取器中选择)。更新UITableView单元格和SwiftyPickerPopover的问题

,我(内侧的UITableView)使用用于此的视图控制器的代码:

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委托:

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

我使用SWIFT 3.

回答

0

我相信错误来自于对你想要做什么以及你在做什么的误解。

你想:改变一个细胞

一个值,你这样做:在所有细胞中的细胞创造改变一个值。

当你拨打:self.tableView.reloadData()unc btnExpenseTypePressed(at index: IndexPath)的iOS调用

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

您还建立索引0DataManager.costsCenter[0]这将意味着,在创建所有的细胞都会有相同的值。

我的建议:不要在你的变化:

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 
    } 
相关问题