skip to Main Content

I have two UITableViewCells. I am trying to create a UI in the second uitableviewcell when the action of a button in the first tableview cell is triggered. I tried delegates but it didn’t work.

protocol ProductColorTableViewCellDelegate {
    func changeUnit(selectedColor: String, _ modelArray : [UnitAndColors])
}

class ProductColorTableViewCell: UITableViewCell { 
var delegate: ProductColorTableViewCellDelegate?

 @objc private func selectColor(_ sender : UIButton) {
    let tag : Int = sender.tag
    guard let model : UnitAndColors = self.arrayUnitsAndColor?[tag] else { return }
    delegate?.changeUnit(selectedColor: model.colorDesc ?? "", arrayUnitsAndColor ?? [])
}
}

Second tableviewcell

class PackagingTableViewCell: UITableViewCell { 

let productColorTableViewCell = ProductColorTableViewCell()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    productColorTableViewCell.delegate = self
    
}
}

extension PackagingTableViewCell: ProductColorTableViewCellDelegate {
    func changeUnit(selectedColor: String, _ modelArray: [UnitAndColors]) {
        selectedColorName = selectedColor
        self.unitStackView.removeAllArrangedSubviews()
        self.arrayUnitsAndColor?.removeAll()
        self.arrayUnitsAndColor = modelArray
        let filteredArray = self.arrayUnitsAndColor?.unique { $0.colorDesc == selectedColorName }
        var i : Int = 0
        filteredArray?.forEach { units in
            let vw = self.createUnits(units, tag: i)
            self.unitStackView.addArrangedSubview(vw)
            vw.snp.makeConstraints {
                $0.size.equalTo(40.0)
            }
            i = i + 1
        }
    }
}

2

Answers


  1. class ProductColorTableViewCell: UITableViewCell { 
    var delegate: ProductColorTableViewCellDelegate?
     @IBOutlet weak var selectColorButtonOL: UIButton!
     @IBAction func selectColor(_ sender : UIButton) {
        let tag : Int = sender.tag
        guard let model : UnitAndColors = self.arrayUnitsAndColor?[tag] else { return }
        delegate?.changeUnit(selectedColor: model.colorDesc ?? "", arrayUnitsAndColor ?? [])
    }
    }
    
    extension ViewController: UItableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ProductColorTableViewCell") as! ProductColorTableViewCell
          cell.delegate = self
          cell.selectColorButtonOL.tag = indexPath.row
            return cell
        }
    }
    
    extension ViewController: ProductColorTableViewCellDelegate{
    func changeUnit(selectedColor: String, _ modelArray: [UnitAndColors]) {
            // implement what you want
        }
    }
    
    Login or Signup to reply.
  2. You can get the next cell by passing indexPath.row + 1 to tableView.cellForRowAt() and then call your function that does the required task.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search