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
You can get the next cell by passing indexPath.row + 1 to tableView.cellForRowAt() and then call your function that does the required task.