Swift UITextField 文本清除 TableViewCell 中的按钮操作 [英] Swift UITextField text clears with button action in TableViewCell
问题描述
我已经录制了我的屏幕并且这里是它的链接.进入细节
I have recorded my screen and here is the link to it. Going into details
- 我有一个 tableView 部分,其中有一个 tableviewcell (PhoneCell) 和两个文本字段(一个是选择器视图输入,另一个是文本字段)和一个添加按钮.
- 当点击添加按钮时,我将一个空电话附加到电话阵列(重复使用相同的 PhoneCell)并调用
tableView.reloadData()
方法.
- 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.
问题:(
每当我点击添加按钮时,我输入的 textFields 文本都会被清除.我想保留它,并且最终应该能够在我点击 Save_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 值
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
}
}
}
编辑(添加 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 协议类型的变量
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!
推荐答案
我要回答我自己的问题 ;-)
I am going to answer my own question ;-)
TextFeilds 只是容纳一些数据(文本)的框.由于每次添加新行时我都会重用 Phonecell,因此我输入的文本会被删除.这是因为它不是存储/保存"在用户对象中.
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.
所以在我的 addPhoneButtonAction 方法中,我添加了这个 -
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!)
}
其中 myIndexPath 是行的 indexPath :-)
Where myIndexPath is the indexPath of the row :-)
工作 示例在这里
这篇关于Swift UITextField 文本清除 TableViewCell 中的按钮操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!