iosswiftuitableviewrx-swiftrxdatasources

How to allow editing of a table view backed by RxDataSources?


I am building a table view backed by RxDataSources. I want to enable editing for this table view, such that the user can delete items.

I currently have this code:

var strings = [String]() {
    didSet {
        observable.accept(strings)
    }
}

let observable = BehaviorRelay(value: [String]())
let disposeBag = DisposeBag()

override func viewDidLoad() {
    tableView.dataSource = nil
    let dataSource = RxTableViewSectionedAnimatedDataSource<StringSection>(configureCell:  {
        (dataSource, collectionView, indexPath, string) -> UITableViewCell in
        let cell = collectionView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = string
        return cell
    })

    observable.asObservable()
        .map {
            [StringSection(items: $0)]
        }
        .bind(to: self.tableView.rx.items(dataSource: dataSource))
        .disposed(by: disposeBag)

    // X

    strings = ["Item 1", "Item 2", "Item 3"]
}

To make this editable, I added this in the place marked X:

tableView.rx.itemDeleted
    .subscribe(onNext: { _ = self.strings.remove(at: $0.row) })
    .disposed(by: disposeBag)
navigationItem.rightBarButtonItem = editButtonItem

And also overrode this method:

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    return .delete
}

However, when I press the edit button, nothing changes for the table view cells. I see no red "-" button on the left. I cannot swipe the cell to the left to reveal the delete button either.

What else do I need to do to enable editing?


Solution

  • I think somewhere in your project has set UITableView in editing mode. Following a snippet code, for example, in Github, allow editing in UITableView like this:

        extension EditingExampleViewController {
        static func dataSource() -> RxTableViewSectionedAnimatedDataSource<NumberSection> {
            return RxTableViewSectionedAnimatedDataSource(
                animationConfiguration: AnimationConfiguration(insertAnimation: .top,
                                                                       reloadAnimation: .fade,
                                                                       deleteAnimation: .left),
                configureCell: { (dataSource, table, idxPath, item) in
                    let cell = table.dequeueReusableCell(withIdentifier: "Cell", for: idxPath)
                    cell.textLabel?.text = "\(item)"
                    return cell
                },
                titleForHeaderInSection: { (ds, section) -> String? in
                    return ds[section].header
                },
                canEditRowAtIndexPath: { _, _ in
                    return true
                },
                canMoveRowAtIndexPath: { _, _ in
                    return true
                }
            )
        }
    }
    

    I set UITableView in editing mode is false and I can swipe left to delete the cell