[英]How to go about multiple textfields in a tableview cell swift
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?如何通过访问每个 tableview 单元格中的每个文本字段将所有数据保存到数组中?
You can create a function to loop table,get data and append ExerciseSet,您可以创建一个 function 来循环表,获取数据和 append 练习集,
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.