2017-02-16 6 views
0

Проблема заключается в том, что когда я выбираю некоторое значение (в полевом переключателе -SwiftyPickerPopover-) в строке, действие обновляет одно и то же значение (выбранное в подборщике) для всех строк.Проблемы с обновлением ячейки UITableView и SwiftyPickerPopover

код, который я использую для этого для ViewController (с 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. 

Вы также установить индекс для 0 в DataManager.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 
    } 
Смежные вопросы