Table 视图单元格不显示更新的数据

Table view cell doesn't show updated data

我达到了正确的值并在调试会话期间打印了它。但是,当我 运行 应用程序时,计算值 (newcalory) 不会显示特定的 table 单元格文本字段。 (又名。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() 将其反映到 UI.

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

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)

    //...
}

希望对您有所帮助。

我找到了解决方案,下面列出的行没有用。

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

我用 plusAction 函数中的新值更新了成分数组,我的问题就解决了。感谢所有帖子。