緣由
iOS開發,我們常用的組件UITableView,UICollectionView,通過代理的方式實現數據的加載,如果我們在UIViewController委托代理,會導致代碼的冗余,同時Controller過于龐大,不利于代碼的閱讀
Objective-C時代我們通過MVVM框架,實現Controller的瘦身,但是仍然會存在很多冗余的代碼
Swift時代我們可以在MVVM的基礎上更好的實現Controller的瘦身,減少冗余的代碼
具體的實現方式可以參考MGTV-Swift,覺得不錯賞個??
實現
ViewModelProtocol
- 定義通用的協議,主要用于CollectionViewMode和TableViewModel
protocol ViewModelProtocol {
associatedtype DataType //定義用語TableView,CollectionView的數據模型
var datas: [[DataType]] { get set } //用于存儲數據的數組,二維數組,滿足多個section的要求
associatedtype ViewType //定義View的類型,主要有UICollectionView,UITableView
associatedtype CellType //定義Cell的類型,主要有UITableViewCell,UICollectionViewCell
func cellConfig(_ view: ViewType, datas: [[DataType]], indexPath: IndexPath) -> CellType //定義方法,用于返回cell
}
- 協議中并未定義Identifier來獲取需要展示的cell,cell的獲取部分交由外部進行提供,這樣可以更靈活的適用于多樣式的cell展示
CollectionViewModel
- 通過typealias重定義UICollectionView的協議
typealias CollectionViewProtocol = UICollectionViewDelegate & UICollectionViewDataSource & UICollectionViewDelegateFlowLayout
- 因為extension不能的一些限制,所以通用的CollectionViewModel采用OOP的方式實現
class CollectionViewModel<T>: NSObject, ViewModelProtocol, CollectionViewProtocol {
typealias DataType = T
typealias ViewType = UICollectionView
typealias CellType = UICollectionViewCell
var datas: [[T]] = []
//子類需要重寫這個方法返回已經UICollectionViewCell
func cellConfig(_ view: UICollectionView, datas: [[T]], indexPath: IndexPath) -> UICollectionViewCell {
return UICollectionViewCell()
}
//可根據子類的樣式重寫需要的屬性,主要用于Cell的樣式
var minimumLineSpacing: CGFloat { return 0 }
var minimumInteritemSpacing: CGFloat { return 0 }
var insetForSection: UIEdgeInsets { return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) }
var itemSize: CGSize { return CGSize(width: 0, height: 0) }
init(_ collectionDatas: [[T]]) {
datas = collectionDatas
super.init()
}
//MARK: - UICollectionView DataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return datas.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return datas[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return cellConfig(collectionView, datas: datas, indexPath: indexPath)
}
//MARK: UICollectionView DelegateFlowlayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//默認是page類型
let itemSizeWith = itemSize.width == 0 ? collectionView.bounds.size.width : itemSize.width
let itemSizeHeight = itemSize.height == 0 ? collectionView.bounds.size.height : itemSize.height
return CGSize(width: itemSizeWith, height: itemSizeHeight)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return insetForSection
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return minimumLineSpacing
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return minimumInteritemSpacing
}
}
TableViewModel
- 通過typealias重定義UITableView的協議
typealias TableViewProtocol = UITableViewDelegate & UITableViewDataSource
class TableViewModel<T>: NSObject, ViewModelProtocol, TableViewProtocol {
typealias DataType = T
typealias ViewType = UITableView
typealias CellType = UITableViewCell
var datas: [[T]] = []
func cellConfig(_ view: UITableView, datas: [[T]], indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
func caculateHeight(_ indexPath: IndexPath) -> CGFloat {
return 0
}
var cellHeight: CGFloat = -1
init(_ tableDatas: [[T]]) {
datas = tableDatas
super.init()
}
//MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
guard cellHeight > 0 else {
return caculateHeight(indexPath)
}
return cellHeight
}
//MARK: - UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int {
return datas.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datas[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return cellConfig(tableView, datas: datas, indexPath: indexPath)
}
}