问题
I have recorded my screen and here is the link to it. Going into details
- I have a tableView section, in which there is a tableviewcell (PhoneCell) with two textfields (one is a pickerview input and other a textfield), and an Add Button.
- When Add button is tapped, I am appending an empty phone to the array of phones (reusing the same PhoneCell) and calling
tableView.reloadData()method.
The Problem :(
Whenever I tap on Add button, the textFields text that I enter is cleared. I would like to retain it and ultimately should be able to save data (number of phones the user entered) when I click Save_Button
Add Phone Button action code:
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)
}
}
In my PhoneCell.swift class, I have the below method that is taking care of data and indexPath values
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
}
}
}
EDIT (ADDED 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()
}
EDIT 2 (added addPhoneButtonAction)
@IBAction func addPhoneButtonAction(sender: AnyObject) {
self.dataSource?.addUserPhone(userInfo!)
}
Where dataSource is variable of type PhoneCellDataSource protocol
protocol PhoneCellDataSource : class {
func fetchCountryTeleCodeList(completion:((XtraResult<NSArray, XtraError>) -> Void))
func addUserPhone(userData: MyUserData)
}
I can share more inputs/code as required. Please help. Thanks in Advance!
回答1:
I am going to answer my own question ;-)
TextFeilds are only boxes that accommodate some data (text). And since I was reusing the Phonecell each time I add a new row, the text I entered gets erased. This is because it is not "stored/saved" in the user object.
So in my addPhoneButtonAction method, I added this -
@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 is the indexPath of the row :-)
The working sample is here
来源:https://stackoverflow.com/questions/41394099/swift-uitextfield-text-clears-with-button-action-in-tableviewcell