我正在学习组合,我想使用组合而不是单元格和表格视图之间的委托。我已成功连接并接收信息,但问题是当重复使用单元时,每次生成相同的事件时,我收到的次数与之前在该重复使用的单元中使用的次数一样多。
我已在视图控制器中将可取消项声明为
var cancellables: Set<AnyCancellable> = []
Run Code Online (Sandbox Code Playgroud)
这是 cellForRow 方法
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: MyCell.celdaReuseIdentifier, for: indexPath)
as? MyCell else {
return MyCell()
}
cell.index = indexPath
cell.lbTitle.text = String("Cell \(indexPath.row)")
cell.tapButton.compactMap{$0}
.sink { index in
print("tap button in cell \(index.row)")
}.store(in: &cancellables)
return cell
}
Run Code Online (Sandbox Code Playgroud)
细胞是
class MyCell: UITableViewCell {
static let cellNibName = "MyCell"
static let celdaReuseIdentifier = "MyCellReuseIdentifier"
@IBOutlet weak var lbTitle: UILabel!
@IBOutlet weak var …Run Code Online (Sandbox Code Playgroud)