代码之家  ›  专栏  ›  技术社区  ›  Timofey Solonin

订阅UIButton.rx。点击位于UITableViewDataSource中的UITableViewCell

  •  4
  • Timofey Solonin  · 技术社区  · 8 年前

    假设我有一个 UIButton 在一个 UITableViewCell . 将单元格从 UITableView UIButton.rx.tap 问题是,如果 UITableViewCell 则订阅将保留。目前,我通过分配 Disposable UITableViewCell ,在创建订阅时设置它,并调用 Disposable.dispose() 在…上 UITableViewCell.prepareForReuse() 然而,据我所知,以一种需要您调用 一次性处理 意味着你做了错事。

    有没有更好的方法来实现订阅的唯一性而无需重新分配 按钮 ?

    2 回复  |  直到 8 年前
        1
  •  13
  •   Michał Ciuba Chandru    8 年前

    Disposable.dispose() )是有一个 DisposeBag 在单元格中,并在中重新创建 prepareForReuse ,如本 GitHub issue :

    //in the cell 
    
    private(set) var disposeBag = DisposeBag()
    
    override func prepareForReuse() {
       super.prepareForReuse()
       disposeBag = DisposeBag()
    }
    
    
    //in the data source
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! DiaryItemCell
    
    cell.commentButton.rx_tap
                .subscribeNext{
    
                }.addDisposableTo(cell.disposeBag)
    
    return cell
    

    如果您的单元格中有更多的按钮(或您想要订阅的其他可观察对象),它也会工作。您不必创建新的 Disposable

        2
  •  1
  •   Artem Novichkov    8 年前

    你可以使用 Cell-Rx pod表单正确,使用中的反应订阅 UITableViewCell .对于您的情况,您可以使用 rx_reusableDisposeBag ,它将正确处理您的订阅。

    推荐文章