gpt4 book ai didi

ios - TableView 单元格不显示更新的数据

转载 作者:行者123 更新时间:2023-11-30 12:44:28 24 4
gpt4 key购买 nike

我达到了正确的值并在调试 session 期间打印了它。但是,当我运行应用程序时,计算值(新卡路里)不会显示特定的表格单元格文本字段。 (又名 cell.itemTotalCalory.text)您对解决方案有什么想法吗?

*我在下面附上了相关的代码块。

非常感谢,

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{

let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! IngredientTableViewCell

cell.ingredientNameTextField.text = ingredients [indexPath.row].ingredientName
cell.numberofItem.text = "1"
let cellcalory = ingredients [indexPath.row].ingredientCalory
cell.itemTotalCalory.text = cellcalory

cell.plusButton.tag = Int(cell.itemTotalCalory.text!)! //indexPath.row
cell.plusButton.addTarget(self, action:#selector(plusAction), for: .touchUpInside)
cell.minusButton.tag = Int(cell.itemTotalCalory.text!)!
cell.minusButton.addTarget(self, action:#selector(minusAction), for: .touchUpInside)

return cell
}


@IBAction func plusAction(sender: UIButton)
{

let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell") as! IngredientTableViewCell
let buttonRow = sender.tag

if cell.numberofItem.text == "1" || cell.numberofItem.text != "1"
{
cell.numberofItem.text = "1"
let textValue1 = cell.numberofItem.text
var textValue = Int(textValue1!)
textValue = textValue! + 1
cell.numberofItem.text = String(describing: textValue)

let oldcalory = buttonRow
cell.itemTotalCalory.text = String (((textValue! * Int(oldcalory)) + Int(oldcalory)))
let newcalory = cell.itemTotalCalory.text

refresh(newcalory: newcalory!);

}
}

func refresh(newcalory :String)
{

let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell") as! IngredientTableViewCell
cell.itemTotalCalory.text = newcalory

DispatchQueue.main.async {
self.ingredientTableView.reloadData()
}
}

最佳答案

您应该做的是更新 ingredients 数组中的值,然后调用 ingredientTableView.reloadData() 将其反射(reflect)到 UI。

调用dequeueReusableCell(withIdentifier:)refresh 方法中将无法按预期执行您想要执行的操作:

For performance reasons, a table view’s data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. Call this method from your data source object when asked to provide a new cell for the table view. This method dequeues an existing cell if one is available or creates a new one using the class or nib file you previously registered. If no cell is available for reuse and you did not register a class or nib file, this method returns nil.

所以,刷新方法应该类似于:

func refresh() {
// updating ingredients array upon reqs satisfaction...
// and then:
ingredientTableView.reloadData()

// nameOfYourRefreshControl.endRefreshing()
}

此外,如果您非常确定要从 tableView 获取特定单元格,则可能需要使用 cellForRow(at:)实例方法:

Returns the table cell at the specified index path.

func refresh() {
let cell = ingredientTableView?.cellForRow(at: YOUR_INDEX_PATH)

//...
}

希望这有帮助。

关于ios - TableView 单元格不显示更新的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41806835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com