0
votes

The first text field is a Int Value where you insert your current sold. A second one textfield where I get some Int value and add to a tableView with a button. The value you add into the second textfield soustract to the main value ( in that case 2500 ). That's working great.

My problem is to attribuate the PickerValue to the TableRow Value. it's working but not like I want.

I want to save the pickerValue when i click onto the button. But when i enter a new value it's changing all row of the pickerValue. In that case, you can see two row with "telephone". Normaly it's would be "telephone" and something else ( "maison" or "house").

Do you see a reason why it's doing like this ? I wish you can understand what i mean.

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

}

Here is some code I'm using. I'm thinking the problem came from the 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
}

Here is my outlet :

@IBOutlet weak var tableRessourceOT: UITableView!

Then my arrayRessource :

    var arrayRessource: [Int] = [] {
    didSet {
        if oldValue != arrayRessource {
            userDefault.setValue(arrayRessource, forKey: arrayKey)
        }
    }
}

and call with the function :

    func getArray() {
    if let newArray = userDefault.array(forKey: arrayKey) as? [Int] {
        arrayRessource = newArray
    }
}
2
in my viewdidLoad have the deleage like that : picker.delegate = self picker.dataSource = self - azics
How do you populate arrayRessource? - vadian
Your picker view's didSelectRow method saves the user's selected row in a variable '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 your arrayRessource` which feeds your table view, and you don't explain how the values in your picker view (2500) relate to the entries in your table view (-2 Euro, etc). You need to provide a lot more, and clearer, information about how your app is supposed to work. (Edit your question, don't add comments.) - Duncan C
How can i populate my Int TableView with PickerView String ? i think it's here my problem what do you think about it ? - azics
Please make the effort to translate your code to english for those unfamiliar with french. I would recommend writing code in english to begin with. - de.

2 Answers

0
votes

Your question is not very clear.
However I will make an attempt to offer some help from what I can extract from your code.

I assume restValueLabel is not something within your cells? It is probably the green label "2500" in your screenshot?
If that is the case, don't set it in cellForRow(at:). This method is called every time a cell becomes visible and should only be used to configure that very cell based on your data source (arrayRessource).

Instead create a method updateRestValueLabel() that iterates your data source, does the calculation and sets the appropriate value to your restValueLabel. Call that method every time your data source changes, so along with tableRessourceOT.reloadData().

There are also some stylistic issues with your code (e.g. avoid force unwrapping ! unless you have good reason to use it) but this is out of scope here.

0
votes

The problem come from my array was an Int array and my pickerValue was String array.

Array can't be String array's and Int array's.

    var arrayRessource: [String] = [] {
    didSet {
        if oldValue != arrayRessource {
            userDefault.setValue(arrayRessource, forKey: arrayKey)
        }
    }
}

Thanx for your advice by the way i'm progressing and doing my best