iosswiftuitableviewrx-swiftrxdatasources

RxDataSources itemDeselected not getting called


I'm using RxDataSources and trying to make a "toggle" function for cell selection — i.e., to allow cells (in a single-selection tableView) to be selected and deselected by tapping. I'm using tableView.rx.itemSelected to get the selection, and self?.tableView.deselectRow(at: indexPath, animated: false) to deselect the cell, if it is currently in the selected state. This seems to basically work, but I want to respond to the deselection. I was assuming that self?.tableView.deselectRow(at: indexPath, animated: false) would result in tableView.rx.itemDeselected being called, but that never happens. I'm obviously missing something.

Any thoughts appreciated.


Solution

  • This is related to RxSwift. itemDeselected is triggered from the UITableView method UITableViewDelegate.tableView(_:didDeselectRowAt:) here.

    According to the apple docs,deselectRow(at:animated:) does not trigger the delegate function tableView(_:didDeselectRowAt:).

    This is why it isn't being triggered.

    I'd say your best bet is to abstract the logic for cell deselection and use it in both places. You could also call the delegate method yourself but that normally isn't a recommended solution.