将 tableViewCell 与 UITextFields 重用可提供来自数组的相同数据
Reusing tableViewCell with UITextFields gives same data from an Array
我有一个带有自定义 tableViewCell 的部分,这个单元格有两个文本字段。
此单元格用于显示 phone 个用户的号码 - 一个文本字段中的国家代码和另一个文本字段中的号码。
我从一项服务获取数据,如果用户有多个 phone,根据 phone 的计数,我正在更新 numberOfRowsInSection 和 cellForRowAtIndexPath。到这里我没有任何问题。
例如,如果用户有两个 phone,我在 numberOfRowsInSection 中 return 2,并显示两个单元格(具有两个文本字段的相同自定义单元格)。
问题:
我在两个单元格中得到相同的数据——第二个 phone 数字详细信息。但是我需要在我的自定义单元格文本字段中一个接一个地显示 phone 个数字列表。
这是我的代码:
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 循环来获取可用的数字列表,但是如何将每个值分配给单元格的文本字段?
请帮忙!
提前致谢
因为在配置方法中你知道单元格的索引路径为什么不直接使用
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
}
}
但无论如何,最好的解决方案是为每个单元格建立模型并将其传递给此配置方法。
numberOfRows 方法中的逻辑实现错误。
在该方法中,根据您的响应数据设置行数。
我有一个带有自定义 tableViewCell 的部分,这个单元格有两个文本字段。 此单元格用于显示 phone 个用户的号码 - 一个文本字段中的国家代码和另一个文本字段中的号码。
我从一项服务获取数据,如果用户有多个 phone,根据 phone 的计数,我正在更新 numberOfRowsInSection 和 cellForRowAtIndexPath。到这里我没有任何问题。
例如,如果用户有两个 phone,我在 numberOfRowsInSection 中 return 2,并显示两个单元格(具有两个文本字段的相同自定义单元格)。
问题: 我在两个单元格中得到相同的数据——第二个 phone 数字详细信息。但是我需要在我的自定义单元格文本字段中一个接一个地显示 phone 个数字列表。
这是我的代码:
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 循环来获取可用的数字列表,但是如何将每个值分配给单元格的文本字段?
请帮忙! 提前致谢
因为在配置方法中你知道单元格的索引路径为什么不直接使用
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
}
}
但无论如何,最好的解决方案是为每个单元格建立模型并将其传递给此配置方法。
numberOfRows 方法中的逻辑实现错误。 在该方法中,根据您的响应数据设置行数。