如何允许编辑 RxDataSources 支持的表视图?

Swe*_*per 1 uitableview ios swift rx-swift rxdatasources

我正在构建一个由 RxDataSources 支持的表视图。我想启用此表视图的编辑,以便用户可以删除项目。

我目前有这个代码:

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"]
}
Run Code Online (Sandbox Code Playgroud)

为了使其可编辑,我将其添加到标记为 X 的位置:

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

并且还重写了这个方法:

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

但是,当我按下编辑按钮时,表视图单元格没有任何变化。我在左侧没有看到红色的“-”按钮。我也无法将单元格向左滑动以显示删除按钮。

我还需要做什么才能启用编辑?

Qua*_*Dam 5

我认为您项目中的某个地方已设置UITableViewediting模式。例如,在Github中的代码片段之后,允许UITableView像这样进行编辑:

    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
            }
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

我设置UITableViewediting模式是false,我可以向左滑动来删除单元格