Ios 如何使用MVVM在UITableViewCell中处理UICollectionView
我正在开发一个具有以下视图层次结构的应用程序:Ios 如何使用MVVM在UITableViewCell中处理UICollectionView,ios,swift,uitableview,mvvm,uicollectionview,Ios,Swift,Uitableview,Mvvm,Uicollectionview,我正在开发一个具有以下视图层次结构的应用程序: ViewController->包含UITableView->包含CustomTableViewCell->包含UICollectionView->包含CustomCollectionViewCell 现在,我已经创建了一个与ViewController对应的ViewModel。ViewModel包含CustomTableViewCells的模型对象,即要显示的CustomTableViewCells的数量以及每个CustomTableViewCe
ViewController
->包含UITableView
->包含CustomTableViewCell
->包含UICollectionView
->包含CustomCollectionViewCell
现在,我已经创建了一个与ViewController
对应的ViewModel
。ViewModel
包含CustomTableViewCells
的模型对象,即要显示的CustomTableViewCells
的数量以及每个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
}
}
}
}
我想知道如何使用MVVM处理UICollectionView
ViewController
作为UITableView
和uicollectionview
的委托和数据源CustomCollectionViewCells
的模型对象保存在哪里?在相同的视图模型中
还是我应该制作一个不同的在这种情况下,我可以得出如下结论: 您应该创建3个
ViewModels
ViewController
CustomTableViewCellView
CustomCollectionViewCellView
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]
}
}
class CustomCollectionViewCellViewModel {
var book: Book!
init(book: Book) {
self.book = book
}
var bookName: String? {
return self.book.name
}
}
最后,CustomCollectionViewCellViewModel
将如下所示
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]
}
}
class CustomCollectionViewCellViewModel {
var book: Book!
init(book: Book) {
self.book = book
}
var bookName: String? {
return self.book.name
}
}
请注意,如果读者需要更多信息(他们知道该问什么),没有必要问他们,我们会问,这些问题会减少聊天和细节(我们更喜欢中性的技术写作)。我经常发布这样的建议:我们轻轻地劝阻问候,希望你能帮助,谢谢,提前感谢,感谢信,问候,亲切的问候,签名,请你能帮助,闲聊的材料和缩写的txtspk,恳求,你被困多久了,投票建议,元评论等等。只要解释一下你的问题,展示你的尝试,你的期望,以及实际发生的事情。