![](/img/trans.png)
[英]text field become blank when table View Scrolled in two Section Swift
[英]text field become Empty when table View Scrolled 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()
在你的 tableview 單元格設置模型數據之后
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
這個方法中。
UITextField
肯定會變空,因此您沒有使用輸入到該UITextField
的數據更新您的contactModel
。 您的方法textFieldDidEndEditing
看起來容易出錯,但是最后您需要在該方法中更新contactModel
所有必需屬性。
例如:
contactModel.givenName = textField.text
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.