简体   繁体   中英

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?

输入的数据可能是什么样子

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM