我们目前正在为一个项目建立架构,我很难想象一个完整的解决方案来解决这个问题。 所以我们目前有一个包含多个动态原型(prototype)的 Collection View ,我们为每个单元格将其中一个原型(prototype)子类化。我想知道我们是否可以按照以下方式做一些事情
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: viewModel.reuseIdentifierForIndexPath(indexPath), for: indexPath)
//update cell here in a generic way based on the class of this cell (we have this information)
return cell
}
基本上,我们想避免做这样的事情
if indexPath.row == 0 {
//do stuff for this specific cell
}
内部方法如 collectionView(_:cellForItemAt:)/collectionView(_:didSelectItemAt:),同时也符合 MVVM 模式。
最佳答案
您可以对单元格本身使用 MVVM 模式。所以每个单元格都会有自己的 viewModel。然后你可以使用类似下面的代码:
class ViewModel {
func reuseIdentifier(for indexPath: IndexPath) -> String {
//...
}
func cellViewModel(for indexPath: IndexPath) -> BaseCellViewModel {
//...
}
}
class BaseCellViewModel {
//...
}
class CellAViewModel: BaseCellViewModel {
//...
}
class CellBViewModel: BaseCellViewModel {
//...
}
class CellA: UICollectionViewCell {
var viewModel: CellAViewModel! {
didSet {
//update UI
}
}
}
class CellB: UICollectionViewCell {
var viewModel: CellBViewModel! {
didSet {
//update UI
}
}
}
class ViewController: UIViewController {
//...
var viewModel: ViewModel = ViewModel()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let reuseIdentifier = viewModel.reuseIdentifier(for: indexPath)
let cell =
collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
//update cell here in a generic way based on the class of this cell (we have this information)
configure(cell: cell, indexPath: indexPath)
return cell
}
func configure(cell: UICollectionViewCell, indexPath: IndexPath) {
switch cell {
case let cell as CellA:
cell.viewModel = viewModel.cellViewModel(for: indexPath) as! CellAViewModel
case let cell as CellB:
cell.viewModel = viewModel.cellViewModel(for: indexPath) as! CellBViewModel
default:
fatalError("Unkown cell type")
}
}
//...
}
关于ios - 具有多个原型(prototype)单元格的 Collection View 以及在 MVVM 下处理它们的通用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41876754/