简体   繁体   中英

Swift - Updating a label in a cell after another cell is loaded?

Could somebody tell me how to achieve the following?

I have 2 cells:

  1. My first cell is loaded and contains a label .

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

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

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!

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.

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? .

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)
}

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
    }
}

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

cell2.delegate = self

in order to receive the delegate calls.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM