我录制了我的屏幕和here is the link to it。 详细介绍
tableView.reloadData()
方法。 问题:(
每当我点击“添加”按钮时,我输入的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值
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
}
}
}
编辑(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()
}
编辑2(添加了addPhoneButtonAction)
@IBAction func addPhoneButtonAction(sender: AnyObject) {
self.dataSource?.addUserPhone(userInfo!)
}
其中dataSource
是PhoneCellDataSource协议类型的变量
protocol PhoneCellDataSource : class {
func fetchCountryTeleCodeList(completion:((XtraResult<NSArray, XtraError>) -> Void))
func addUserPhone(userData: MyUserData)
}
我可以根据需要分享更多输入/代码。请帮忙。在此先感谢!
答案 0 :(得分:0)
我将回答我自己的问题; - )
TextFeild只是容纳某些数据(文本)的框。由于每次添加新行时我都在重复使用Phonecell,因此我输入的文本将被删除。这是因为它不是&#34; 存储/保存&#34;在用户对象中。
所以在我的addPhoneButtonAction方法中,我添加了这个 -
@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: - )