【问题标题】:RxSwift + canMoveRowAtRxSwift + canMoveRowAt
【发布时间】:2018-12-19 14:41:27
【问题描述】:

我正在使用 RxSwift (RxCocoa) 来填充我的 tableView 单元格

viewModel.cellsDriver.drive(tableView.rx.items) { ... }

这样我就无法访问tableView的dataSource的方法了

func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool

但我需要指定哪些行可以移动

tableView.rx.itemMoved

如何限制哪些行不能拖动?我宁愿避免使用 RxDataSources

【问题讨论】:

    标签: swift uitableview rx-swift rx-cocoa


    【解决方案1】:

    为了做到这一点,您需要做两件事之一。要么拉入 RxDataSource Cocoapod 并使用 TableViewSectionedDataSource 子类之一,要么创建自己的数据源。

    制作数据源并不难。下面是一个例子:https://github.com/dtartaglia/RxMultiCounter/blob/master/RxMultiCounter/RxExtensions/RxSimpleAnimatableDataSource.swift 我在我的中使用了 DifferenceKit,但是如果你想要超级简单,你可以在你的 func tableView(_:observedEvent) 方法中调用 tableView.reloadData()

    这是一个例子:

    //
    //  RxTableViewMovableRowDataSource.swift
    //
    //  Created by Daniel Tartaglia on 12/19/18.
    //  Copyright © 2018 Daniel Tartaglia. MIT License.
    //
    
    import UIKit
    import RxSwift
    import RxCocoa
    
    
    class RxTableViewMovableRowDataSource<E, Cell: UITableViewCell>: NSObject, RxTableViewDataSourceType, UITableViewDataSource {
    
        init(identifier: String, configure: @escaping (Int, E, Cell) -> Void, canMoveRowAt: ((Int, E) -> Bool)? = nil) {
            self.identifier = identifier
            self.configure = configure
            self.canMoveRowAt = canMoveRowAt
        }
    
        func tableView(_ tableView: UITableView, observedEvent: Event<[E]>) {
            values = observedEvent.element ?? []
            tableView.reloadData()
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return values.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! Cell
            let row = indexPath.row
            configure(row, values[row], cell)
            return cell
        }
    
        func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
            let row = indexPath.row
            return canMoveRowAt?(row, values[row]) ?? true
        }
    
        let identifier: String
        let configure: (Int, E, Cell) -> Void
        let canMoveRowAt: ((Int, E) -> Bool)?
        var values: Element = []
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 2019-06-30
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多