2016-12-27 57 views
0

我有一個自定義tableViewCell的一節,這個單元格有兩個文本字段。 此單元用於在一個文本字段中顯示用戶國家代碼的電話號碼,並在其他文本字段中顯示該號碼。重用tableViewCell與UITextFields給出了一個數組相同的數據

我從服務中獲取數據,如果用戶有多個電話,根據電話的數量,我正在更新numberOfRowsInSection和cellForRowAtIndexPath。我在這裏沒有任何問題。

例如,如果用戶有兩部手機,我在numberOfRowsInSection中返回2,並顯示兩個單元格(具有兩個textField的相同自定義單元格)。

問題: 我在兩個單元格中都得到相同的數據 - 第二個電話號碼詳細信息。但我需要在我的自定義單元格文本框中一個接一個地顯示電話號碼列表。

這裏是我的代碼

numberOfRowsInSection

public override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

      switch section { 

      case 3:     
       return (user.phones?.count)! 

      default: 
       return 1 

      } 
    } 

的cellForRowAtIndexPath

public override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    case .ContactPhones: 
      if contactCellExpanded == true { 
       cell = tableView.dequeueReusableCellWithIdentifier("PhoneCell") as? PhoneCell 

      } 
     case .ContactEmail: 
      if contactCellExpanded == true { 
       cell = tableView.dequeueReusableCellWithIdentifier("EmailCell") as? EmailCell 
      } 

     default: 
      break 


} 

PhoneCell

import UIKit 

class PhoneCell: ProfileCell { 

    @IBOutlet weak var countryCode: UITextField! 

    @IBOutlet weak var addPhoneButton: UIButton! 
    @IBOutlet weak var teleNumber: UITextField! 


    @IBAction func addPhoneButtonAction(sender: AnyObject) { 

    } 

    override func configure(withUser user: XtraUser, language: String, indexPath: NSIndexPath) { 
     super.configure(withUser: user, language: language, indexPath: indexPath) 

     self.countryCode.borderStyle = .RoundedRect 
     self.teleNumber.borderStyle = .RoundedRect 

     if let userPhoneInfo = user.phones { 
      for i in 0..<userPhoneInfo.count { 
       print ("numbers \(userPhoneInfo[i].number)") 
       self.countryCode.text = userPhoneInfo[i].country 
       self.teleNumber.text = userPhoneInfo[i].number 
      } 
     } 
    } 
} 

據我所知,我使用for循環來獲取數字列表可用時,但如何將每個值分配給單元格的textField?

請幫忙! 由於提前

回答

2

由於配置方法,你知道的單元格的索引路徑你爲什麼不只是使用

override func configure(withUser user: XtraUser, language: String, indexPath: NSIndexPath) { 
    super.configure(withUser: user, language: language, indexPath: indexPath) 

    self.countryCode.borderStyle = .RoundedRect 
    self.teleNumber.borderStyle = .RoundedRect 

    print("Enumerated \(user.phones?.enumerate())") 

    if let userPhoneInfo = user.phones { 
     self.countryCode.text = userPhoneInfo[indexPath.row].country 
     self.teleNumber.text = userPhoneInfo[indexPath.row].number 
    } 
} 

但無論如何,最好的解決辦法是讓模型對每個細胞並將它傳遞給這個配置方法。

+0

這很簡單。沒有擊中我的愚蠢的大腦! :'( –

+0

我有一個延伸的問題,我的原始問題,你能幫我嗎? 用戶也可以添加電話 - 我有一個添加按鈕,增加「user.phones?.count」的數量。在添加時,它會清除textFields中的值,但無法找出原因! –

+0

好了,在配置方法中加入了斷點,並檢查點擊添加按鈕後會發生什麼,也許在某些時候你用空字符串填充文本字段,它必須被調試。 –

0

numberOfRows方法中錯誤的邏輯實現。 在該方法中根據您的數據設置行數作爲響應。

+0

它的確會返回來自響應數據的行數,我不確定你是否指出其他的東西。 ponse。但更新我的單元格的文本字段中的數據很困難。 –

+0

看,你已經按照章節設置了開關盒。但我認爲你只有一個部分,所以不應該有切換案例。 –

+0

我只顯示與我的PhoneCell相關的情況。但是還有另外4種情況,沒有在我的問題中發帖以避免大量的代碼行 –

相關問題