首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何允许编辑RxDataSources支持的表视图?

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

Stack Overflow用户
提问于 2019-08-26 09:32:11
回答 1查看 1.1K关注 0票数 1

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

我现在有这样的代码:

代码语言:javascript
复制
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的位置添加了以下内容:

代码语言:javascript
复制
tableView.rx.itemDeleted
    .subscribe(onNext: { _ = self.strings.remove(at: $0.row) })
    .disposed(by: disposeBag)
navigationItem.rightBarButtonItem = editButtonItem

并且还覆盖了这个方法:

代码语言:javascript
复制
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    return .delete
}

但是,当我按下编辑按钮时,表格视图单元格没有任何变化。我在左边看不到红色的"-“按钮。我也不能滑动左边的单元格来显示删除按钮。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-26 15:01:54

我想您的项目中的某个地方已经将UITableView设置为editing模式。例如,在Github中的代码片段之后,允许在UITableView中进行如下编辑:

代码语言:javascript
复制
    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模式下的UITableView设置为false,可以向左滑动来删除单元格

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57650927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档