Ios RxSwift:如何使用ViewModel在表视图内的集合视图单元格中填充数据?

Ios RxSwift:如何使用ViewModel在表视图内的集合视图单元格中填充数据?,ios,swift,mvvm,rx-swift,Ios,Swift,Mvvm,Rx Swift,我有以下结构: - TableView -- Custom Table View Cell --- CollectionView ---- Custom CollectionView Cell 我想了解如何使用RxSwift-MVVM结构从/使用此结构中的视图模型传递数据 每当我从API获得响应时,它都应该分别更新表视图行和关联的集合视图单元格中的数据。这里是我刚才编写的一个示例,演示如何使用RxSwift来完成您想要的任务重要提示:这是一个粗略的示例,没有经过最佳编写和测试!我只是用一个文本

我有以下结构:

- TableView
-- Custom Table View Cell
--- CollectionView
---- Custom CollectionView Cell
我想了解如何使用RxSwift-MVVM结构从/使用此结构中的视图模型传递数据


每当我从API获得响应时,它都应该分别更新表视图行和关联的集合视图单元格中的数据。

这里是我刚才编写的一个示例,演示如何使用RxSwift来完成您想要的任务重要提示:这是一个粗略的示例,没有经过最佳编写和测试!我只是用一个文本编辑器写的,希望它能帮到你,如果没有,我会在有更多时间的时候尝试完善它

class MyViewModel {
    // Lets say TableData is your model for the tableView data and CollectionData for your collectionView
    public let tableData : PublishSubject<[TableData]> = PublishSubject()
    public let collectionData : PublishSubject<[CollectionData]> = PublishSubject()

    private let disposeBag = DisposeBag()

    func fetchData() {
        // Whenever you get an update from your API or whatever source you call .onNext
        // Lets assume you received an update and stored them on a variable called newShopsUpdate 
        self.tableData.onNext(newTableDataUpdate)
        self.collectionData.onNext(newCollectionDataDataUpdate)
    }
}

class MyViewController: UIViewController {
    var tableData: BehaviorRelay<[TableData]> = BehaviorRelay(value: [])
    var collectionData:  BehaviorRelay<[CollectionData]> = BehaviorRelay(value: [])
    let viewModel = MyViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Setup Rx Bindings
        viewModel
            .tableData
            .observeOn(MainScheduler.instance)
            .bind(to: self.tableData)
            .disposed(by: DisposeBag())
        viewModel
            .collectionData
            .observeOn(MainScheduler.instance)
            .bind(to: self.collectionData)
            .disposed(by: DisposeBag())

        // Register yours Cells first as usual
        // ...
        // Setting the datasource using RxSwift
        tableData.bind(to: tableView.rx.items(cellIdentifier: "yourCellIdentifier", cellType: costumeTableViewCell.self)) { row, tableData, cell in 
            // Set all the cell properties here
            // Lets also assume you have you collectionView inside one of the cells
            cell.tableData = tableData

            collectionData.bind(to: cell.collectionView.rx.items(cellIdentifier: "yourCellIdentifier", cellType: costumeCollectionViewCell.self)) { row, collectionData, cell in 
                // Set all the cell properties here
                cell.collectionData = collectionData
            }.disposeBag(by: DisposeBag())
        }.disposed(by: DisposeBag())
    }
}
类MyViewModel{
//假设TableData是tableView数据的模型,CollectionData是collectionView的模型
public let tableData:PublishSubject=PublishSubject()
公共租赁收集数据:PublishSubject=PublishSubject()
私有出租dispebag=dispebag()
func fetchData(){
//无论何时从API或调用.onNext的任何源获得更新
//假设您收到了一个更新,并将其存储在一个名为newShopsUpdate的变量中
self.tableData.onNext(newTableDataUpdate)
self.collectionData.onNext(newCollectionDataUpdate)
}
}
类MyViewController:UIViewController{
var tableData:BehaviorRelay=BehaviorRelay(值:[])
var collectionData:BehaviorRelay=BehaviorRelay(值:[])
让viewModel=MyViewModel()
重写func viewDidLoad(){
super.viewDidLoad()
//设置Rx绑定
视图模型
.表格数据
.observeOn(MainScheduler.instance)
.bind(到:self.tableData)
.disposed(由:DisposeBag()处理)
视图模型
.收集数据
.observeOn(MainScheduler.instance)
.bind(到:self.collectionData)
.disposed(由:DisposeBag()处理)
//像往常一样先注册你的手机
// ...
//使用RxSwift设置数据源
tableData.bind(到:tableView.rx.items(cellIdentifier:“yourCellIdentifier”,cellType:CustomTableViewCell.self)){row,tableData,中的单元格
//在此处设置所有单元格属性
//我们还假设您的collectionView位于其中一个单元格内
cell.tableData=tableData
collectionData.bind(到:cell.collectionView.rx.items(cellIdentifier:“yourCellIdentifier”,cellType:CustomeCollectionViewCell.self)){row,collectionData,中的单元格
//在此处设置所有单元格属性
cell.collectionData=collectionData
}.disposeBag(by:disposeBag())
}.disposed(由:DisposeBag()处理)
}
}

最简单的解决方案是使用数组数组

比如说。让我们假设您的API返回:

struct组:可解码{
let items:[字符串]
}
然后,您的视图模型将如下所示:

func tableViewItems(来源:Observable)->Observable{
返回源
.map{$0.map{$0.items}
}
创建单元格时,可以使用
observable.just()
将内部数组包装成一个observable,如下所示:

//例如,在视图控制器的viewDidLoad中。
tableViewItems(来源:apiResponse)
.bind(到:tableView.rx.items(cellIdentifier:“Cell”,cellType:CollectionTableViewCell.self)){u0,元素,中的单元格
可观察的。只是(元素)
.bind(到:cell.collectionView.rx.items(cellIdentifier:“cell”,cellType:UICollectionViewCell.self)){u0,元素,单元格中的单元格
让label=(cell.viewWithTag(6969)作为?UILabel)??UILabel()
label.tag=6969
label.text=元素
label.sizeToFit()
cell.addSubview(标签)
}
.处置(由:cell.disposeBag)
}
.已处理(由:dispsoeBag)