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

标签 ios swift xcode uitableview

我达到了正确的值并在调试 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/

相关文章:

swift - Swift 中的 CLGeocoder - 使用 reverseGeocodeLocation 时无法返回字符串

ios - 如何在 XCode 中安装特定的 iPhone 模拟器 SDK 版本?

ios - UIViewController 与键 XXXXX 的键值编码不兼容。 IB 中没有错误的连接

ios - 如何使用此代码在崩溃前捕获错误

ios - 有什么可能的方法来获取来电或去电号码以及所有日志详细信息?

ios - UISearchController 搜索栏没有获得焦点

ios - 在 Firebase 中迭代快照子项

ios - Swift 在 UITableView 中添加页脚 View

ios - 更新查询对象上的解析 ACL

ios - 根据实际可见字母高度在 UILabel 中垂直居中文本