如何更改视图,同时仍允许用户在TextField中进行编辑?

时间:2018-03-15 12:42:26

标签: ios swift uitableview swift3 uitextfield

所以我有一组电话号码并且我遍历它,如果数组中的一个与textField中的一个相同,我将重新加载表格行并显示一些数字正确的图像类似的东西。否则,让我们说如果用户现在更改了那个好的数字,比如删除小数,现在数字不再与数组中的数字相匹配,所以我需要更改那个单元格视图。但是这个当前代码重新加载我的表对于textField中的每个更改,通过重新加载,它将不再保持编辑模式。

    @objc func textFieldValueCahnged(_ textField: UITextField) {

    if textField.tag == 0 {
        if !shouldValidatePhone {
            for phone in self.phoneNumbers {
                if phone == textField.text! {
                   self.phoneNumber = phone
                    GlobalMainQueue.async {
                        self.phoneValidated = true
                       // self.reloadCellForIdentifier("ExtraFieldCell")
                        self.mainTableView.reloadRows(at: [self.rowindexpath!], with: .fade)
                    }
                    break
                } else {
                    GlobalMainQueue.async {
                        self.phoneValidated = false
                        self.phoneNumber = textField.text!
                        self.mainTableView.reloadRows(at: [self.rowindexpath!], with: .fade)
                        // TODO:  IF IT 'S typed a different number that the ones in self.phoneNumbers array we need to update the view with the cell.elementsAreHidden()
                    }
                    break
                }
            }
        }
    } else {
        verifyCode = textField.text!
    }
}

tableView单元格:

let cell = tableView.dequeueReusableCell(withIdentifier: "PhoneValidationCell") as! PhoneValidationCell

                    self.rowindexpath = indexPath
                    cell.label.text = "\(currentField.label):"

                    cell.textField.addTarget(self, action: #selector(textFieldValueCahnged(_:)), for: .editingChanged)
                    cell.sendCodeBtn.addTarget(self, action: #selector(sendCodeButtonPressed(_:)), for: .touchUpInside)

                    cell.textField.tag = 0
                    cell.sendCodeBtn.tag = 0

                    cell.textFieldForVeryfing.addTarget(self, action: #selector(textFieldValueCahnged(_:)), for: .editingChanged)
                    cell.verifyCodeBtn.addTarget(self, action: #selector(sendCodeButtonPressed(_:)), for: .touchUpInside)

                    cell.textFieldForVeryfing.tag = 1
                    cell.verifyCodeBtn.tag = 1

                    if self.phoneNumbers.isEmpty {
                        if let phoneNumbers = currentField.userPhones {
                            self.phoneNumbers = phoneNumbers
                        }
                    }

                    if !phoneValidated && !shouldValidatePhone {
                        if let value = self.extraFieldsValues[currentField.name] as? String {
                            for i in phoneNumbers {
                                if i == value {
                                    cell.phoneVerified()
                                    break
                                } else {
                                    cell.elementsAreHidden()
                                }
                            }
                            if phoneNumbers.isEmpty {
                                cell.elementsAreHidden()
                            }
                            cell.label.textColor = UIColor.black
                            cell.textField.text = value
                            self.phoneNumber = value

                        } else {
                            cell.label.textColor = Theme.placeholderColor()
                            cell.textField.text = ""
                        }
                    } else {
                       // cell.phoneVerified() /// check here
                        cell.textField.text = self.phoneNumber
                    }

                    if shouldValidatePhone && !phoneValidated {
                        cell.phoneToBeVerified()
                    }

                    if phoneValidated && shouldValidatePhone {
                        cell.phoneVerified()
                    }

基本上当phoe号码在textField中时我显示cell.elementsAreHidden()。当需要验证号码cell.phoneToBeVerified()时。当它验证cell.phoneVerified()

1 个答案:

答案 0 :(得分:1)

您应该存储正在编辑的单元格的indexPath,并在重新加载后通过调用cell.textField.becomeFirstResponder()再次关注它的文本字段。

例如:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath == self.rowindexpath {
        (cell as? ProjectTableViewCell)?.textField.becomeFirstResponder()
    }
}