小编Spi*_*vmp的帖子

使用组合重复值将 UITableviewCell 与 UITableview 连接起来

我正在学习组合,我想使用组合而不是单元格和表格视图之间的委托。我已成功连接并接收信息,但问题是当重复使用单元时,每次生成相同的事件时,我收到的次数与之前在该重复使用的单元中使用的次数一样多。

我已在视图控制器中将可取消项声明为

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)

swift swift5 combine

4
推荐指数
1
解决办法
2711
查看次数

标签 统计

combine ×1

swift ×1

swift5 ×1