简体   繁体   English

tableViewCell MVVM中的CollectionView

[英]CollectionView in tableViewCell MVVM

I am building an app in Swift 4 using MVVM non reactive. 我正在使用无响应MVVM在Swift 4中构建应用程序。 I would like to call viewmodel updates from the tableView's View model in order not to turn it into spagetthi code. 我想从tableView的View模型中调用viewmodel更新,以免将其转换为spagetthi代码。 Instead of creating one view model for each collectionViewCell I am trying to update from main ViewModel. 我没有为每个collectionViewCell创建一个视图模型,而是尝试从主ViewModel更新。

//Spaghetti option (VM - ViewModel , VC - ViewController) 
//TableVC - TableVM 
//Cell- CellVM 
//CollectionCell- CollectionVM 

And models and managers updating. 以及模型和管理者的更新。 I am using the method below to delegate back to the Main TableViewController to give the MainViewModel all responsibility. 我正在使用下面的方法将其委派回Main TableViewController,以赋予MainViewModel所有责任。 Since I have multiple CollectionViews in different cells I think this is the best way to do. 由于我在不同的单元中有多个CollectionView,因此我认为这是最好的方法。 Any suggestions? 有什么建议么?

func setCollectionViewDataSourceDelegate<D: UICollectionViewDataSource & UICollectionViewDelegate>(dataSourceDelegate: D, forRow row:Int){
    self.collectionView.delegate = dataSourceDelegate
    self.collectionView.dataSource = dataSourceDelegate
    self.collectionView.tag = row
    self.collectionView.reloadData()
}

You're really much better off creating a viewmodel for each cell. 为每个单元创建一个视图模型确实要好得多。 The spaghetti part comes when you do the binding. 当您进行装订时,意大利面部分出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM