2016-12-30 65 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

相關問題