2016-12-30 66 views
3

我记录了我的屏幕和here is the link to it。 进入细节Swift UITextField文本与TableViewCell中的按钮动作清除

  1. 我有一个的tableView部分,在其中有一个tableviewcell(PhoneCell)有两个文本框(一个是pickerview输入等一个文本框),并添加按钮。
  2. 点击添加按钮时,我将一个空电话附加到电话阵列(重复使用相同的PhoneCell)并调用tableView.reloadData()方法。

的问题:(
每当我添加按钮点击,我进入被清除文本框的文本。我想保留它最终应该能够保存数据(数字电话用户进入)当我点击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 

      } 
     } 
    } 

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(加入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

精美制作的GIF的解决方案嘉黎.. 。:)请问你可以查阅你的cellForRowAtIndexPath代码吗? –

+0

@DheerajD:谢谢:-) 我在我的问题中添加了cellForRowAtIndexPath –

+0

@LohithKorupolu你可以显示'addPhoneButton'的按钮动作吗? –

回答

0

我要回答我的问题;-)

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

所以在我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工作sample is here

相关问题