Ios Tableview需要重新加载两次才能从textfield更新数据?
我对桌面视图有疑问 这是我的tableView代码Ios Tableview需要重新加载两次才能从textfield更新数据?,ios,swift,uitableview,Ios,Swift,Uitableview,我对桌面视图有疑问 这是我的tableView代码 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tierCount } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tierCount
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "InterestRateTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? InterestRateTableViewCell else {
fatalError("The dequed cell is not an instance of InterestRateTableViewCell.")
}
cell.interestRateTextField.delegate = self
cell.rowLabel.text = "\(indexPath.row + 1)."
if let interestText = cell.interestRateTextField.text {
if let interest = Double(interestText){
interestRateArray[indexPath.row] = interest
} else {
interestRateArray[indexPath.row] = nil
}
} else {
interestRateArray[indexPath.row] = nil
}
return cell
}
如您所见,我使用cellForRowAt方法从单元格中的文本字段中获取值,并分配给数组。(我实际上每个单元格有2个文本字段。)
基本上,我让用户输入并编辑文本字段,直到他们满意为止,然后单击这个计算按钮,这将调用计算方法。在计算方法中,我首先调用“tableView.reloadData()”从文本字段收集数据,然后再进行实际计算 问题是当我运行应用程序时。我在所有文本字段中键入值,然后单击“计算”,但它显示错误,就像文本字段仍然为空一样。我再次点击,它成功了。就好像我必须重新装填两次才能让事情顺利进行 有人能帮我吗 顺便说一下,请原谅我的英语。我不是来自讲英语的国家 编辑:根据某人的建议,在这里发布计算按钮代码可能很有用。这是计算按钮的代码
@IBAction func calculateRepayment(_ sender: UIButton) {
//Reload data to get the lastest interest rate and duration values
DispatchQueue.main.async {
self.interestRateTableView.reloadData()
}
//Get the loan value from the text field
if let loanText = loanTextField.text {
if let loanValue = Double(loanText) {
loan = loanValue
} else {
print("Can not convert loan value to type Double.")
return
}
} else {
print("Loan value is nil")
return
}
tiers = []
var index = 0
var tier: Tier
for _ in 0..<tierCount {
if let interestRateValue = interestRateArray[index] {
if let durationValue = durationArrayInMonth[index] {
tier = Tier(interestRateInYear: interestRateValue, tierInMonth: durationValue)
tiers.append(tier)
index += 1
} else {
print("Duration array contain nil")
return
}
} else {
print("Interest rate array contain nil")
return
}
}
let calculator = Calculator()
repayment = calculator.calculateRepayment(tiers: tiers, loan: loan!)
if let repaymentValue = repayment {
repaymentLabel.text = "\(repaymentValue)"
totalRepaymentLabel.text = "\(repaymentValue * Double(termInYear!) * 12)"
} else {
repaymentLabel.text = "Error Calculating"
totalRepaymentLabel.text = ""
}
}
@iAction func calculateRepayment(u发件人:ui按钮){
//重新加载数据以获取最新的利率和期限值
DispatchQueue.main.async{
self.interestRateTableView.reloadData()
}
//从文本字段中获取贷款值
如果让loanText=loanTextField.text{
如果让loanValue=Double(loanText){
贷款=贷款价值
}否则{
打印(“无法将贷款价值转换为类型Double。”)
返回
}
}否则{
打印(“贷款价值为零”)
返回
}
层=[]
var指数=0
风险等级:等级
对于0中的uu..cellForRowAt
用于初始创建和配置每个单元格,因此调用此方法时文本字段为空
UITableView.reloadData()
文档:
// Reloads everything from scratch. Redisplays visible rows. Note that this will cause any existing drop placeholder rows to be removed.
open func reloadData()
正如上面苹果评论中所说,UITableView.reloadData()
将从头开始重新加载所有内容,包括文本字段
有很多方法可以解决您的问题,但是如果没有更多的上下文,很难说最好的方法。下面的示例非常适合您代码的当前上下文:
class MyCustomTableViewCell: UITableViewCell {
@IBOutlet weak var interestRateTextField: UITextField!
var interestRateChangedHandler: (() -> ()) = nil
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
interestRateTextField.addTarget(self, action: #selector(interestRateChanged), for: UIControlEvents.editingChanged)
}
@objc
func interestRateChanged() {
interestRateChangedHandler?()
}
}
和cellforrowatinex
:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "InterestRateTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? InterestRateTableViewCell else {
fatalError("The dequed cell is not an instance of InterestRateTableViewCell.")
}
cell.rowLabel.text = "\(indexPath.row + 1)."
cell.interestRateChangedHandler = { [weak self] in
if let interestText = cell.interestRateTextField.text {
if let interest = Double(interestText){
self?.interestRateArray[indexPath.row] = interest
} else {
self?.interestRateArray[indexPath.row] = nil
}
} else {
self?.interestRateArray[indexPath.row] = nil
}
}
return cell
}
“然后单击此计算按钮”但是你没有显示那个按钮的代码,所以这个问题是没有用的。显示相关的代码!我们不知道你在做什么,直到你这么做。很抱歉,马特。现在,我编辑了这篇文章,包含了那个按钮的代码。谢谢你的解决方案,杰克。我会研究你的答案。我会做一些研究。你的答案是一个好的开始。我非常感谢。不过,请您澄清一下,当我调用reloadData()两次时,它为什么会起作用?