【发布时间】: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,谢谢你的信息,但我有两行以上,我只是分享了我想做的事情。