【问题标题】:How to handle UICollectionView in UITableViewCell with MVVM如何使用 MVVM 处理 UITableViewCell 中的 UICollectionView
【发布时间】:2018-05-18 09:17:40
【问题描述】:

我正在开发一个具有以下视图层次结构的应用:

ViewController -> 包含UITableView -> 包含CustomTableViewCell -> 包含UICollectionView -> 包含CustomCollectionViewCell

现在我创建了一个对应于ViewControllerViewModelViewModel 包含 CustomTableViewCells 的模型对象,即要显示的 CustomTableViewCells 的数量以及要在每个 CustomTableViewCell 中显示的内容。

class ViewModel
{
    //MARK: Private Properties
    private var libraries = [Library](){
        didSet{
            self.reloadTableViewClosure?()
        }
    }

    //MARK: Internal Properties
    var reloadTableViewClosure: (()->())?
    var numberOfLibraries: Int{
        return self.libraries.count
    }

    //MARK: Internal Methods
    func getLibrary(at indexPath: IndexPath) -> Library
    {
        return self.libraries[indexPath.row]
    }

    //MARK: Initializer
    init()
    {
        self.fetchLibraryList()
    }

    //MARK: Private Methods
    private func fetchLibraryList()
    {
        if let path = Bundle.main.path(forResource: "LibraryList", ofType: "json")
        {
            if let libraryList = try? JSONDecoder().decode([Library].self, from: Data(contentsOf: URL(fileURLWithPath: path)))
            {
                self.libraries = libraryList
            }
        }
    }
}

我想知道我应该如何使用 MVVM 处理 UICollectionView

  1. 我应该将主要的ViewController 设为UITableViewUICollectionViews 的委托和数据源吗?

  2. 我应该将CustomCollectionViewCells 的模型对象保存在哪里?在同一个ViewModel 还是我应该做一个不同的?

【问题讨论】:

  • 请注意,没有必要询问读者是否需要更多信息(他们知道要问),我们要求问题排除聊天和细节(我们更喜欢中性的技术写作)。我经常发布这样的建议:我们不鼓励问候、希望你能帮助、谢谢、提前感谢、感谢信、问候、亲切的问候、签名、请你帮忙、聊天材料和缩写txtspk、恳求、你被困多长时间、投票建议、元评论等。只需解释你的问题,并展示你尝试了什么、你期望什么以及实际发生了什么。

标签: ios swift uitableview mvvm uicollectionview


【解决方案1】:

在这种情况下我能想到的如下,

你应该创建 3 个ViewModels

  1. ViewModel 用于ViewController
  2. CustomTableViewCellViewModelCustomTableViewCellView
  3. CustomCollectionViewCellViewModelCustomCollectionViewCellView

这就是你的ViewModels 的样子,

class ViewModel
{
    private var cellVMs = [CustomTableViewCellViewModel] = []

    var reloadTableViewClosure: (()->())?

    var numberOfLibraries: Int {
        return self.cellVMs.count
    }

    func getLibraryCellVM(at indexPath: IndexPath) -> CustomTableViewCellViewModel
    {
        return self.cellVMs[indexPath.row]
    }

    //MARK: Initializer
    init()
    {
        self.fetchLibraryList()
    }

    //MARK: Private Methods
    private func fetchLibraryList()
    {
        if let path = Bundle.main.path(forResource: "LibraryList", ofType: "json")
        {
            if let libraryList = try? JSONDecoder().decode([Library].self, from: Data(contentsOf: URL(fileURLWithPath: path)))
            {
                libraryList.forEach({
                    cellVMs.append(CustomTableViewCellViewModel(library: $0))
                })
                self.reloadTableViewClosure?()
            }
        }
    }
}

您的CustomTableViewCellViewModel 将如下所示,

class CustomTableViewCellViewModel {

        var booksVMs: [CustomCollectionViewCellViewModel] = []

        var library: Library!

        init(library: Library) {
            self.library = library

            // Initialize booksVMs
            library.books.forEach({
                booksVMs.append(CustomCollectionViewCellViewModel.init(book: $0))
            })
        }

        var numberOfBooks: Int {
            self.booksVMs.count
        }

        func bookViewModel(at indexPath: IndexPath) -> CustomCollectionViewCellViewModel {
            return self.booksVMs[indexPath.row]
        }
    }

最后CustomCollectionViewCellViewModel 会是这个样子,

class CustomCollectionViewCellViewModel {

   var book: Book!

   init(book: Book) {
     self.book = book
   }

   var bookName: String? {
      return self.book.name
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    相关资源
    最近更新 更多