我有兩個 UITableViewCells。當第一個 tableview 單元格中的按鈕操作被觸發時,我試圖在第二個 uitableviewcell 中創建一個 UI。我試過委托,但沒有用。
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 ?? [])
}
}
第二個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
}
}
}
uj5u.com熱心網友回復:
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
}
}
uj5u.com熱心網友回復:
您可以通過將 indexPath.row 1 傳遞給 tableView.cellForRowAt() 來獲取下一個單元格,然后呼叫執行所需任務的函式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/399418.html
上一篇:有沒有辦法讓視頻快速重復?
下一篇:SwiftUI計時器未啟動
