【发布时间】:2021-08-27 13:34:02
【问题描述】:
我是 RXSwift 的新手,希望根据可观察的值来控制我的表格视图滑动操作。
我有一个变量 - Observable 并基于 if Product.isEnabled 我想显示“已售罄”或“有货”滑动操作。这是当前代码:
func tableView(
_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath
) -> UISwipeActionsConfiguration? {
self.interactionService.prod
let soldOutAction = UIContextualAction(
style: .normal,
title: "Sold Out"
) { [weak self] (_, _, completionHandler) in
guard let self = self else { return }
self.interactionService.products.map({
$0[indexPath.row]
})
.take(1)
.do(onNext: { [weak self] in
guard let self = self else { return }
self.analytics.userMarkedAsSoldOut(product: $0.productID)
})
.flatMapLatest({self.interactionService.disableProduct($0)})
.subscribe(onCompleted: {
completionHandler(true)
})
.disposed(by: self.disposeBag)
}
soldOutAction.backgroundColor = UIColor(red: 0.996,
green: 0.09,
blue: 0.478,
alpha: 1)
let config = UISwipeActionsConfiguration(actions: [soldOutAction])
config.performsFirstActionWithFullSwipe = false
return config
}
上面的代码工作正常。但我需要更新它,以便如果 products[indexPath.row].isEnabled -> 使用 soldOutAction,如果 !isEnabled,则使用新创建的 inStockAction。
如前所述,我对 RXSwift 很陌生,所以我不知道如何更改 RXSwift 语法以使用 soldOutAction 或 inStockAction。非常感谢您对此事的任何帮助。
【问题讨论】: