当表视图滚动 Swift 时,文本字段变为空



VC2 内部有一个表视图,用于修改联系人或将联系人添加到手机中。问题是当表格视图滚动时,文本字段在部分中变为空白。我找到了解决此问题的方法,但我无法正确处理它。

型:

class ContactModel : NSObject {
var identifier : String!
var thumbnailImageData : UIImage?
var givenName : String!
var familyName : String!
var phoneNumbers : [String]!
var emailAddresses : [String]!
override init() {
self.phoneNumbers = []
self.emailAddresses = []
super.init()
}

VC2 :

var contactModel = ContactModel()
@IBOutlet weak var tvInsert: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell0 = tableView.dequeueReusableCell(withIdentifier: "InsertTableCell0") as! InsertTableCell0
cell0.txtFirstName.text = self.contactModel.givenName
cell0.txtLastName.text = self.contactModel.familyName
return cell0
}else if indexPath.section == 1 {
let cell1 = tableView.dequeueReusableCell(withIdentifier: "InsertTableCell1") as! InsertTableCell1
cell1.btnDelete.addTarget(self, action: #selector(deleteRowDate(_:)), for: .touchUpInside)
cell1.txtPhoneNumber.placeholder = "Phone Number"
cell1.txtPhoneNumber.text = contactModel.phoneNumbers[indexPath.row]
// here i get textField tag 
cell1.txtPhoneNumber.tag = indexPath.row
cell1.txtPhoneNumber.delegate = self
return cell1
}else {
let cell2 = tableView.dequeueReusableCell(withIdentifier: "InsertTableCell2") as! InsertTableCell2
cell2.btnEmail.addTarget(self, action: #selector(deleteRowDate(_:)), for: .touchUpInside)
cell2.txtEmail.placeholder = "Email"
cell2.txtEmail.text = contactModel.emailAddresses[indexPath.row]
return cell2
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
let index = IndexPath(row: textField.tag, section: 1)
let cell = tvInsert.cellForRow(at: index) as? InsertTableCell1
let pointInTable = cell?.txtPhoneNumber.convert(textField.bounds.origin, to: self.tvInsert)
let textFieldIndexPath = self.tvInsert.indexPathForRow(at: pointInTable!)
print(textFieldIndexPath)
}

创建您需要的所有字段的模型,并将数组设置为空白,如下所示。

var dict:[String:Any] = [:]
dict["description"] = ""
arrGetEducationList.append(EducationDetailsArrModel(JSON: dict)!)
self.tblEducation.reloadData()

之后在你表视图单元格集模型数据之后,像那样

let edu = self.arrGetEducationList[indexPath.row]
cell.txtSchool.text = edu.edu_institute_name

之后为文本字段创建委托方法

func textFieldDidEndEditing(_ textField: UITextField) {
let pointInTable = textField.convert(textField.bounds.origin, to: self.tblEducation)
let IndexPath = self.tblEducation.indexPathForRow(at: pointInTable)
if let cell = tblEducation.cellForRow(at: IndexPath!) as? AddEducationCell1 {
if cell.txtSchool == textField{
arrGetEducationList[(IndexPath?.row)!].edu_institute_name = textField.text!
}
}
}

您的文本字段显然是重用的。 因此,请使用临时存储以保存临时内容。 例如,您可以使用

yourTextField.addTarget(self, action: #selector(textFieldDidChanged(textField:)), for: .editingChanged)

并抓住:

@objc func textFieldDidChange(textField: UITextField){
storage[textField.tag] = textField.text
}
let storage = [Int: String]()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tag = tagFromIndexPath(indexPath)
textField.text = storage[tag]
}

您还必须在模型中设置文本字段的值。 在textFieldDidEndEditing这种方法。

UITextFields 肯定会为空,因此您没有使用键入该UITextField的数据更新contactModel。您的方法看起来textFieldDidEndEditing容易出错,但最终您需要更新该方法中contactModel的所有需要属性。

例如:

contactModel.givenName = textField.text

最新更新