如何移动逻辑以显示 UITableViewRowAction 从委托方法到视图模型(MVVM 架构)
How to move logic to show UITableViewRowAction from delegate method to View Model (MVVM Architecture)
我正在学习使用 RxSwift 和 RxDataSource 将业务逻辑与视图控制器分离(使用 MVVM)。
假设我想在 iOS 的邮件应用程序中创建 table 视图,当用户向左滑动时,有 删除按钮 和 更多按钮.
在普通 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。因为我认为 show/hide SwipeAction
按钮的逻辑应该在 ViewModel 中。
欢迎提出任何建议。
谢谢。
更新:
我尝试使用 RxDataSources,但仍然没有自定义滑动操作的线索。对于删除操作,我可以使用 tableView.rx.itemDeleted
方法来订阅它。但是如果我想订阅自定义滑动操作(比如我的设为默认操作)。怎么做?
首先,viewModel 应该只包含特定视图显示的数据。所以我不确定 viewModel 是否应该包含有关您的视图行为的信息(在这种情况下,这意味着您的单元格有一些操作)。也许我会把这个逻辑封装到模型本身。我猜你从 API 得到了类似 isBankAccountDefault
的东西。如果您不直接从 API 获取此属性,则需要一些逻辑来确定它。您可以在模型中创建计算 属性 并将其放在 * cellIsNotDefault * logic.
但是如果你坚持要在viewModel中拥有它,你可以在viewModel中创建Bool 属性 isBankAccountDefault
。然后,您将把 logic * cellIsNotDefault * 放入格式化程序,即创建该 viewModel 并将 isBankAccountDefault
值相应地设置为 * cellIsNotDefault * logic.
我正在学习使用 RxSwift 和 RxDataSource 将业务逻辑与视图控制器分离(使用 MVVM)。
假设我想在 iOS 的邮件应用程序中创建 table 视图,当用户向左滑动时,有 删除按钮 和 更多按钮.
在普通 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。因为我认为 show/hide SwipeAction
按钮的逻辑应该在 ViewModel 中。
欢迎提出任何建议。
谢谢。
更新:
我尝试使用 RxDataSources,但仍然没有自定义滑动操作的线索。对于删除操作,我可以使用 tableView.rx.itemDeleted
方法来订阅它。但是如果我想订阅自定义滑动操作(比如我的设为默认操作)。怎么做?
首先,viewModel 应该只包含特定视图显示的数据。所以我不确定 viewModel 是否应该包含有关您的视图行为的信息(在这种情况下,这意味着您的单元格有一些操作)。也许我会把这个逻辑封装到模型本身。我猜你从 API 得到了类似 isBankAccountDefault
的东西。如果您不直接从 API 获取此属性,则需要一些逻辑来确定它。您可以在模型中创建计算 属性 并将其放在 * cellIsNotDefault * logic.
但是如果你坚持要在viewModel中拥有它,你可以在viewModel中创建Bool 属性 isBankAccountDefault
。然后,您将把 logic * cellIsNotDefault * 放入格式化程序,即创建该 viewModel 并将 isBankAccountDefault
值相应地设置为 * cellIsNotDefault * logic.