Swift - 在加载另一个单元格后更新单元格中的标签? [英] Swift - Updating a label in a cell after another cell is loaded?

查看:108
本文介绍了Swift - 在加载另一个单元格后更新单元格中的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何实现以下目标吗?

Could somebody tell me how to achieve the following?

我有2个单元格:


  1. 我的第一个单元格已加载并包含标签

我的第二个单元格已加载并包含另一个标签。该标签是
,填充了来自 URLSession 的值。

My second cell is loaded and contains another label. That label is populated with a value from a URLSession.

我想要做的是,使用单元格2的标签标签 >。

What I want to do is, update the label in cell 1 with the value in cell 2's label.

我不太清楚如何做到这一点。我已经尝试在tableview中创建一个变量来捕获结果并执行didSet然后 tableView.reloadData ,但这会陷入无限循环并且失败。

I'm not quite sure how to do this. I've tried creating a variable in the tableview to capture the result and do a didSet then tableView.reloadData, but that traps me in an endless loop and fails.

我可以发布代码,但是我所描述的并没有什么独特之处,所以如果被问到,只会添加代码。提前致谢!

I can post code, but there is nothing unique from what I've described so will only add code if asked. Thanks in advance!

以下代码,根据要求提供。我希望 runningTotal 的值进入我的 infoCell.totalPriceLabel.text 之后已更新。:

Code below, as requested. I want the value of runningTotal to go into my infoCell.totalPriceLabel.text after it has been updated.:

var runningTotal: Double = Double()

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if cellItems.count == 0 {
        let orderEmptyCell = Bundle.main.loadNibNamed("OrderEmptyTableViewCell", owner: self, options: nil)?.first as! OrderEmptyTableViewCell
        return orderEmptyCell
    } else {
        if indexPath.section == 0 {
            let infoCell = Bundle.main.loadNibNamed("InfoTableViewCell", owner: self, options: nil)?.first as! InfoTableViewCell
            infoCell.totalPriceLabel.text = String(Double(runningTotal))
            print(runningTotal)
            return infoCell
        }else if indexPath.section == 1 {
            let item: BasketModel = cellItems[indexPath.row] as! BasketModel
            let ordersCell = Bundle.main.loadNibNamed("OrderTableViewCell", owner: self, options: nil)?.first as! OrderTableViewCell


            if item.multi == 1 {
                ordersCell.nameLabel.text = item.name! + ": " + item.filling!
            } else {
                ordersCell.nameLabel.text = item.name!
            }

            ordersCell.priceLabel.text = String(Double(basketStruct.getQty(id: item.id!)) * Double(item.price!)!)
            runningTotal += (Double(basketStruct.getQty(id: item.id!)) * Double(item.price!)!)
            ordersCell.quantityLabel.text = String(basketStruct.getQty(id: item.id!))
            return ordersCell
        } 
    }
}


推荐答案

这个问题不是一个特别难以解决的问题,因此我将尝试将其分解为一小部分。根据你所描述的,你大致是在正确的方向上。

This problem isn't a particularly trivial one to solve, so I will try to break it down into small parts. From what you have described, you are roughly on the right lines.

首先,你需要一些方法来跟踪第一个细胞。您可能希望从表视图中使用 tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)委托方法以获取第一个单元格,然后保存它进入该视图控制器中的变量,类似 var firstCell:MyCustomCellClass?

To start, you need some way to keep track of the first cell. You will probably want to use the tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) delegate method from the table view in order to get the first cell, and then save it into a variable in that view controller, something like var firstCell: MyCustomCellClass?.

然后,使用你的想法在第二个单元格内部 didSet ,我们可以使用委托来通知视图控制器任何更改。为此,我将在第二个单元格类中声明一个协议:

Then, using your idea of didSet inside of the second cell, we can use a delegate to inform the view controller of any changes. For this I would declare a protocol inside the second cell class along the lines of:

protocol MySecondCellProtocol {
  func myValueDidChange(newValue: String)
}

然后在你的第二个细胞类中,你需要一个委托变量如下: var delegate:MySecondCellProtocol?以便您可以执行以下操作:

Then inside your second cell class, you will need a delegate variable as so: var delegate: MySecondCellProtocol? so that you can do the following:

var myValue = "" {
    didSet(newVal) {
        delegate?.myValueDidChange(newVal)
    }
}

在视图控制器中,您需要扩展自己以实现此协议:

In your view controller, you will want to extend yourself to implement this protocol:

extension MyViewController: MySecondCellProtocol {
    func myValueDidChange(newValue: String) {
        firstCell?.label.text = newValue
    }
}

您还需要返回 tableView(_ tableView:UITableView, cellForRowAt indexPath:IndexPath) f unction and set:

You will also want to go back to the tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) function and set:

cell2.delegate = self

以便接收代表电话。

如果有任何不清楚的地方,请告诉我。我显然没有包含执行此操作所需的所有代码,因为它将取决于您的方案,并且到目前为止没有看到您的代码库,这是我能做的好。

Let me know if any of this is unclear. I obviously haven't included all of the code necessary to do this as it will depend on your scenario, and without seeing you codebase so far this is as good as I can do.

这篇关于Swift - 在加载另一个单元格后更新单元格中的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆