【问题标题】:Communication between uitableviewcellsuitableviewcells之间的通信
【发布时间】:2022-01-27 16:17:41
【问题描述】:

我有两个 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
        }
    }
}

【问题讨论】:

  • 您可以使用委托。当您单击单元格按钮时,触发单元格中的委托,然后在第二个表格视图单元格中进行所有必需的更改并重新加载表格视图
  • 我尝试使用委托,但它不起作用,我不明白出了什么问题。让我编辑问题并告诉你我做了什么。
  • NotificationCenter怎么样?
  • 您的 tableView 是否只有两行?如果是,听起来您需要重新考虑您正在使用的 UIView 的类型。 UITableViews 用于单列垂直滚动内容。
  • 是的,你是对的@Anwuna,谢谢你的信息,但我有两行以上,我只是分享了我想做的事情。

标签: swift xcode


【解决方案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
    }
}

【讨论】:

  • 我正在尝试更改用户界面,但没有成功。
【解决方案2】:

您可以通过将 indexPath.row + 1 传递给 tableView.cellForRowAt() 来获取下一个单元格,然后调用执行所需任务的函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-20
    • 2016-04-02
    • 1970-01-01
    • 2014-12-26
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多