gpt4 book ai didi

ios - 如何在更改 View 的同时仍然让用户在 TextField 中进行编辑?

转载 作者:行者123 更新时间:2023-11-28 14:56:14 24 4
gpt4 key购买 nike

所以我有一个电话号码数组并遍历它,如果数组中的一个与文本字段中的相同,我将重新加载表格行并显示一些带有数字 ok 之类的图像那。否则让我们说如果用户现在更改那个很好的数字,比如删除一个小数,现在这个数字不再与数组中的数字匹配,所以我需要更改那个单元格 View 。但是这个当前代码重新加载我的表以进行每次更改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()

最佳答案

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

例如:

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

关于ios - 如何在更改 View 的同时仍然让用户在 TextField 中进行编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49299760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com