【问题标题】:How to allow editing of a table view backed by RxDataSources?如何允许编辑由 RxDataSources 支持的表视图?
【发布时间】:2019-08-26 15:01:53
【问题描述】:

我正在构建一个由 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"]
}

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

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

并且还覆盖了这个方法:

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

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

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

【问题讨论】:

    标签: ios swift uitableview rx-swift rxdatasources


    【解决方案1】:

    我认为您项目中的某处已将UITableView 设置为editing 模式。 在一个 sn-p 代码之后,例如,在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
                }
            )
        }
    }
    

    我在editing模式下设置UITableViewfalse,我可以向左滑动删除单元格

    【讨论】:

      猜你喜欢
      • 2017-04-10
      • 1970-01-01
      • 2022-11-10
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多