代码之家  ›  专栏  ›  技术社区  ›  PGDev

如何使用MVVM处理uiTableViewCell中的uiCollectionView

  •  1
  • PGDev  · 技术社区  · 7 年前

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

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

    现在我已经创建了一个 ViewModel 对应于 视图控制器 . 这个 视图模型 包含的模型对象 CustomTableViewCells ,即 自定义表视图单元格 显示内容以及每个内容中要显示的内容 自定义表视图单元格 .

    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
                }
            }
        }
    }
    

    我想知道我该怎么处理 ui集合视图 使用MVVM?

    1. 我要做主菜吗 视图控制器 两者的委托和数据源 uiTable视图 和; UICollectionViews ?

    2. 模型对象应该放在哪里 CustomCollectionViewCells ?在同一个 视图模型 或者我应该换一个?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Kamran    7 年前

    在这种情况下,我能理解的是:

    您应该创建3 ViewModels

    1. 视图模型 对于 ViewController
    2. 自定义表视图单元格视图模型 对于 CustomTableViewCellView
    3. 自定义集合视图单元格视图模型 对于 CustomCollectionViewCellView

    以下是您的 视图模型 应该看起来像,

    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
       }
    }