Reputation: 1136
I want to save some values into an array from multiple textfields in a tableview cell. I realized my current implementation only will work if data is entered in a very specific way. This is what I've tried:
func textFieldDidEndEditing(_ textField: UITextField) {
if(textField.tag == 1){
self.weight = textField.text!
} else if(textField.tag == 2){
self.reps = textField.text!
}
if(self.reps != "" && self.weight != ""){
let set = ExerciseSet(weight: self.weight, reps: self.reps, RPE: "")
self.setsArray[setsArray.count - 1] = set
self.weight = ""
self.reps = ""
}
}
But this implementation would only work if data is entered, then the next cell is added, then entered. How can I save all the data into the array by accessing each textfield in each tableview cell?
Upvotes: 1
Views: 1064
Reputation: 2688
You can create a function to loop table,get data and append ExerciseSet,
func getTableData(){
for i in 0..<tbl.numberOfRows(inSection: 0) { //tbl--> your table name
let cell = tbl.cellForRow(at: IndexPath(row: i, section: 0)) as! TableViewCell. //TableViewCell--> your tableview custom cell name
let set = ExerciseSet(weight: cell.txtWeight.text ?? "", reps: cell.txtReps.text ?? "", RPE: "") //txtWeight,txtReps your 2 text field names
self.setsArray[setsArray.count - 1] = set
}
}
Upvotes: 4