Swift UITextField文本通过TableViewCell中的button操作清除

我logging了我的屏幕, 这里是它的链接 。 进入细节

  1. 我有一个tableView部分,其中有一个tableviewcell( PhoneCell )有两个文本框(一个是pickerviewinput和其他文本框),和一个添加button。
  2. 点击添加button时,我将一个空电话附加到电话arrays(重复使用相同的PhoneCell)并调用tableView.reloadData()方法。

问题 :(
每当我点击添加button,我input的textFields文本被清除。 我想保留它,最终应该能够保存数据(用户input的电话数量),当我点击Save_Button

添加电话button操作码:

 func addUserPhone(userData: MyUserData) { self.user = userData var emptyPhoneDict:[String:String] = ["country":"", "number":""] if (user.phones?.count < 4) { var emptyPhone : MyUserPhoneNumber = MyUserPhoneNumber(dictionary: emptyPhoneDict)! emptyPhone.country = "" emptyPhone.number = "" user.phones?.append(emptyPhone) } } 

在我的PhoneCell.swift类中,我有下面的方法,正在照顾数据和indexPath值

 override func configure(withUser user: MyUserData, language: String, indexPath: NSIndexPath) { super.configure(withUser: user, language: language, indexPath: indexPath) configureCountryCodePicker() fetchCountryTeleCodeList() userInfo = user self.countryCode.borderStyle = .RoundedRect self.teleNumber.borderStyle = .RoundedRect if user.phones?.count == 0 { self.countryCode.text = "" self.teleNumber.text = "" } else { if let userPhoneInfo = user.phones { self.countryCode.text = userPhoneInfo[indexPath.row].country self.teleNumber.text = userPhoneInfo[indexPath.row].number } } } 

编辑(添加cellForRowAtIndexPath)

 public override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { guard let section = Section(rawValue: indexPath.section) else { print("❌ Invalid section index.") return UITableViewCell() } let expanded = expandedCellPaths.contains(indexPath) var cell: ProfileCell? switch section { case .ContactPhones: if contactCellExpanded == true { cell = tableView.dequeueReusableCellWithIdentifier("PhoneCell") as? PhoneCell (cell as? PhoneCell)?.dataSource = self if user.phones?.count == 4 { (cell as! PhoneCell).addPhoneButton.hidden = true } else { (cell as! PhoneCell).addPhoneButton.hidden = false } } case .ContactEmail: if contactCellExpanded == true { cell = tableView.dequeueReusableCellWithIdentifier("EmailCell") as? EmailCell } default: break } cell?.configure(withUser: user, language: languageCode, indexPath: indexPath) cell?.delegate = self return cell ?? UITableViewCell() } 

编辑2(添加addPhoneButtonAction)

 @IBAction func addPhoneButtonAction(sender: AnyObject) { self.dataSource?.addUserPhone(userInfo!) } 

其中dataSource是PhoneCellDataSource协议types的variables

 protocol PhoneCellDataSource : class { func fetchCountryTeleCodeList(completion:((XtraResult<NSArray, XtraError>) -> Void)) func addUserPhone(userData: MyUserData) } 

我可以根据需要分享更多的input/代码。 请帮忙。 提前致谢!

我要回答我自己的问题;-)

TextFeilds只能容纳一些数据(文本)。 而且,由于每次添加新行时都会重用Phonecell,因此我input的文本被删除。 这是因为它没有被“ 存储/保存 ”在用户对象中。

所以在我的addPhoneButtonAction方法中,我添加了这个 –

 @IBAction func addPhoneButtonAction(sender: AnyObject) { userInfo?.phones![myIndexPath!.row].country = countryCode.text! userInfo?.phones![myIndexPath!.row].number = teleNumber.text! self.dataSource?.addUserPhone(userInfo!) } 

where myIndexPath是行的索引path:-)

工作样品在这里