我有一個自定義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?
請幫忙! 由於提前
這很簡單。沒有擊中我的愚蠢的大腦! :'( –
我有一個延伸的問題,我的原始問題,你能幫我嗎? 用戶也可以添加電話 - 我有一個添加按鈕,增加「user.phones?.count」的數量。在添加時,它會清除textFields中的值,但無法找出原因! –
好了,在配置方法中加入了斷點,並檢查點擊添加按鈕後會發生什麼,也許在某些時候你用空字符串填充文本字段,它必須被調試。 –