问题是当我在一行中选择某个值(在字段选择器-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 :(得分:0)
我认为错误来自于对你想做什么以及你正在做什么的误解。
您想:更改一个单元格中的一个值
你这样做:在创建单元格的所有单元格中更改一个值。
在self.tableView.reloadData()
iOS电话
unc btnExpenseTypePressed(at index: IndexPath)
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
}