簡體   English   中英

如何允許編輯由RxDataSources支持的表視圖?

[英]How to allow editing of a table view backed by 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"]
}

為了使它可編輯,我在標記為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
}

但是,當我按下編輯按鈕時,表格視圖單元格沒有任何變化。 我看不到左側的紅色“-”按鈕。 我也無法向左滑動單元格以顯示刪除按鈕。

我還需要做什么才能啟用編輯功能?

我認為您項目中的某處已將UITableView設置為editing模式。 遵循一段代碼,例如在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模式下將UITableView設置為false ,可以向左滑動以刪除單元格

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM