【发布时间】:2021-07-17 09:54:51
【问题描述】:
第一个文本字段是一个 Int Value,您可以在其中插入当前已售出的商品。第二个文本字段,我在其中获取一些 Int 值并使用按钮添加到 tableView。您添加到第二个文本字段中的值将提取到主值(在这种情况下为 2500 )。效果很好。
我的问题是将 PickerValue 归因于 TableRow 值。它工作正常,但不像我想要的那样。
我想在单击按钮时保存 pickerValue。但是当我输入一个新值时,它会改变pickerValue的所有行。在这种情况下,您可以看到两行带有“电话”的信息。通常它是“电话”和其他东西(“maison”或“house”)。
你看到它这样做的原因吗?我希望你能明白我的意思。
problem with picker value save into table view (gif)
@IBAction func addBtn(_ sender: Any) {
view.endEditing(true)
salaire = Int(salaireLabel.text!) ?? 0
valeur = Int(ressourceTextField.text!) ?? 0
if String(valeur) != "" {
addBtnActivated = true
restValueLabel.textColor = UIColor.blue
arrayRessource.append(valeur)
// modifie automatiquement le salaire
restValueLabel.text = String(soustraction)
tableRessourceOT.reloadData()
// reset le champs
ressourceTextField.text = ""
picker.selectRow(0, inComponent: 0, animated: true)
}
}
这是我正在使用的一些代码。我认为问题来自 tableview Func
var arrayPickerValue: [String] = ["", "maison", "telephone"]
// ------------------------------ START PICKER ------------------------------------
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
print("-----arraypicker.count------")
print(arrayPickerValue.count)
return arrayPickerValue.count
}
// hauteur du picker pour que les images ne se supperpose pas / Picker height
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 25
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
print("----arraypicker[row]----")
print(arrayPickerValue[row])
return arrayPickerValue[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
stored = arrayPickerValue[row]
print("-----stored dans func didselectrow------")
print(stored as Any)
}
//-------------------------- END PICKER ----------------- ------------------
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let row = indexPath.row
let value = arrayRessource[row]
cell.textLabel?.text = "- " + String(value) + " €"
// cell.detailTextLabel?.text =
updateRestLabel()
setRestLabel()
return cell
}
这是我的出路:
@IBOutlet 弱变量 tableRessourceOT: UITableView!
然后是我的 arrayRessource :
var arrayRessource: [Int] = [] {
didSet {
if oldValue != arrayRessource {
userDefault.setValue(arrayRessource, forKey: arrayKey)
}
}
}
并使用函数调用:
func getArray() {
if let newArray = userDefault.array(forKey: arrayKey) as? [Int] {
arrayRessource = newArray
}
}
【问题讨论】:
-
在我的 viewdidLoad 中有这样的委托:picker.delegate = self picker.dataSource = self
-
您的选择器视图的
didSelectRow方法将用户选择的行保存在变量“stored. You never reference that variable anywhere else in your code. You never show the button code that is supposed to add a picker value to your table view, you never show how you changed yourarrayRessource”中,该变量为您的表视图提供数据,您没有解释选择器视图中的值如何( 2500)与表格视图中的条目相关(-2 欧元等)。你需要提供更多、更清晰的关于你的应用程序应该如何工作的信息。 (编辑您的问题,不要添加 cmets。) -
如何使用 PickerView 字符串填充我的 Int TableView ?我认为这是我的问题你怎么看?
标签: ios swift tableview picker