我正在学习使用 RxSwift 和 RxDataSource 将业务逻辑与视图控制器分离(使用 MVVM)。
假设我想在 iOS 的邮件应用程序中创建表格视图,当用户向左滑动时,有删除按钮和更多按钮。
在普通的 MVC 中,我必须实现委托方法editActionsForRowAt
。
public func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
var swipeActions = [UITableViewRowAction]()
let removeAction = UITableViewRowAction(style: .destructive, title: "Delete") { [weak self] (action, indexPath) in
guard let strongSelf = self else { return }
strongSelf.deleteBankAccountTrigger.onNext(indexPath)
tableView.deleteRows(at: [indexPath], with: .fade)
}
swipeActions.append(removeAction)
if *cellIsNotDefault* {
let makeDefaultAction = UITableViewRowAction(style: .default, title: "Make Default") { [weak self] (action, indexPath) in
guard let strongSelf = self else { return }
strongSelf.makeBankAccountDefaultTrigger.onNext(indexPath)
}
makeDefaultAction.backgroundColor = UIColor.lightGray
swipeActions.append(makeDefaultAction)
}
return swipeActions
我不知道如何将逻辑cellIsNotDefault从委托移动到 ViewModel。因为我认为显示/隐藏SwipeAction
按钮的逻辑应该在 ViewModel 中。
欢迎任何建议。
谢谢你。
更新:
我尝试使用 RxDataSources,但仍然没有自定义滑动操作的线索。对于删除操作,我可以使用tableView.rx.itemDeleted
方法订阅它。但是,如果我想订阅自定义 Swipe 操作(例如我的 Make Default 操作)。怎么做?