How To get values of textfields from a UITableViewCell?

后端 未结 6 1657
天命终不由人
天命终不由人 2021-01-01 02:13

So I have thisUITableView cell that has 4 UITextField, and I want to get their values on button click.

This code does not retrieve any valu

6条回答
  •  情话喂你
    2021-01-01 02:46

    I suppose your tableViewCells are not dynamic. Your cell method will looks like this.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellID") as! YourClass
        cell.yourTextField.tag = indexPath.row
    //
    // Alias will be tag 0
    // Primary Phone will be tag 1
    // Secondary Phone will be tag 2 
    // Email Address will be tag 3, so on and so forth
    // Then, attach to each delegate
    //
        cell.yourTextField.delegate = self
    }
    

    Inside your UITableView handling class (which conforms to UITextFieldDelegate protocol), write this.

    func textField(_ textField: UITextField, shouldChangeCharactersIn range:NSRange, replacementString string: String) -> Bool {
    let kActualText = (textField.text ?? "") + string
    switch textField.tag
    {
    case 0:
        aliasValueHolder = kActualText;
    case 1:
        primaryPhoneValueHolder = kActualText;
    case 2:
        secondaryPhoneValueHolder = kActualText;
    case 3:
        emailAddressValueHolder = kActualText;
    default:
        print("It is nothing");
    }
    return true;
    }
    

    Then you can submit it.

提交回复
热议问题