How to go about multiple textfields in a tableview cell swift
我想将一些值从tableview单元中的多个文本字段保存到数组中。 我意识到,仅当以非常特定的方式输入数据时,我当前的实现才有效。 这是我尝试过的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 ="" } } |
但是,只有在输入数据,然后添加下一个单元格然后再输入时,此实现才有效。 如何通过访问每个tableview单元中的每个文本字段将所有数据保存到数组中?
您可以创建一个函数来循环表,获取数据并追加ExerciseSet,
1 2 3 4 5 6 7 | 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 } } |