ios - 具有多个原型(prototype)单元格的 Collection View 以及在 MVVM 下处理它们的通用方法

标签 ios swift mvvm uicollectionview

我们目前正在为一个项目建立架构,我很难想象一个完整的解决方案来解决这个问题。 所以我们目前有一个包含多个动态原型(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/

相关文章:

ios - 有没有办法在 iPad 上为表单启用视差?

iphone - 具有线性渐变的 Chrome 文本效果提供锯齿状边缘

c# - 如何使用 wpf mvvm 在表单中生成(绑定(bind))测验

c# - MVVM + 多级/嵌套数据绑定(bind)

ios - 将本地包中的图像发布到 Pinterest iphone。

ios - 表情符号不显示在推送通知中

ios - 错误 : type may not reference itself as a requirement associatedtype Suffix: SuffixableContainer where Suffix. 项目 == 项目

ios - 如何使用 UITableViews 通过 Swift 创建静态和动态行

c# - WPF MVVM 在您键入时更改文本框大小

当我构建并运行应用程序时,iPhone 模拟器屏幕是黑色的