How to get the values of multiple textFields in UITableView with dynamic cells?

前端 未结 2 535
攒了一身酷
攒了一身酷 2021-01-26 00:28

In a UIViewController I have a UITableView with 1 Dynamic Prototype cell of class type TextInputTableViewCell.
In the Content View of the prototype cell I am a

2条回答
  •  没有蜡笔的小新
    2021-01-26 00:59

    You can try this.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TextInputCell") as! TextInputTableViewCell
    
        cell.textField.isEnabled = placeHolderItems[indexPath.row].editable
        cell.textField.placeholder = placeHolderItems[indexPath.row].name
    
    
        if !self.userInfo.isEmpty{
    
            switch indexPath.row {
            case 0:
                cell.textField.text = self.userInfo[0].FirstName + " " + self.userInfo[indexPath.row].LastName
            case 1:
                cell.textField.text = self.userInfo[0].PhoneNumber
    
            case 2: cell.textField.text = self.userInfo[0].EmailAddress
    
            case 3: cell.textField.text = self.userInfo[0].FlatNumber
    
            case 4: cell.textField.text = self.userInfo[0].StreetAddress
    
            case 5: cell.textField.text = self.userInfo[0].PostCode
    
            default:
                break
            }
        }
    
        // Add tag to cell textField as cell current index
        cell.textField.tag = indexPath.row
        //Observer changes as user edits
        cell.txtField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
    
        return cell
    }
    
    //MARK: - TextField Delegate
    func textFieldDidChange(textfield: UITextField) {
        print("TextField \(String(describing: textfield.text)) Tag : \(textfield.tag)")
    
        switch textfield.tag {
        case 0:
            let Name  = textfield.text // or store where ever you want in sepearte array or update in userInfo
        case 1:
            let PhoneNumber   = textfield.text
    
        case 2:
            let EmailAddress   = textfield.text
    
        case 3:
            let FlatNumber   = textfield.text
    
        case 4:
            let StreetAddress   = textfield.text
    
        case 5:
            let PostCode  = textfield.text
        default:
            break
        }
    
    }
    
    // Don't forget to add UITextFieldDelegate
    

提交回复
热议问题