ModuleServices
系统要求
适用于iOS 12及以上。需要Swift 5.5和XCode 11.0或更高版本。
安装
Swift打包管理器
您可以通过在您的Package.swift
中添加以下行来使用Swift打包管理器进行安装:
import PackageDescription
let package = Package(
[...]
dependencies: [
.Package(name: "ModuleServices", url: "https://github.com/cosmicfools/ModuleServices.git", .branch("master"))
]
)
CocoaPods
您可以通过在您的Podfile
中添加以下行来使用CocoaPods进行安装:
pod 'ModuleServices'
如何使用它
ModuleServices基本是一个帮助您快速开发的工具包。这个库对基于UITableView的UIViewControllers非常有用。该库的主要概念是,模块等于UITableView中的一个分区,因此有一个名为ModulesViewController的UIViewController子类来管理所有模块。
模块类似于一个精简的UIViewController,应该能够独立工作。
基本上,ModulesViewController有一个包含TableSectionModules的数组。
有时候,一个例子比千言万语更容易理解。因此,我们有一个额外的充满例子存储库:Module-examples
1. 创建一个模块
您需要创建一个TableSectionModule
的子类。
class FirstSectionModule: TableSectionModule {}
2. 在模块中重写所需的方法
有很多可以重写的方法。最常见的包括:
- 使用
Class
/Nib
注册UITableViewCell
/UITableViewHeaderFooterView
override func registerClassForCells() -> [AnyClass]
override func registerClassForHeadersFooters() -> [AnyClass]
override func registerNibsForCells() -> [AnyClass]
override func registerNibsForHeadersFooters() -> [AnyClass]
- 创建行,这就像是
UITableView
的数据源
override func createRows()
- 重新利用并配置
UITableViewCell
override func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell
-
其他可以重写的方法您可以基本上重写与
UITableViewDelegate
和UITableViewDataSource
提供的基本相同的方法。 -
显然,您将需要创建并配置模块将包含的所有
UITableViewCells
。
TableSectionModule
示例
带有方法的import ModuleServices
class FirstSectionModule: TableSectionModule {
override func registerNibsForCells() -> [AnyClass] {
return super.registerNibsForCells() + [
Example1TableViewCell.classForCoder(),
]
}
override func registerClassForCells() -> [AnyClass] {
super.registerClassForCells() + [UITableViewCell.classForCoder()]
}
override func createRows() {
super.createRows()
rows.append(String(describing: Example1TableViewCell.self))
rows.append(String(describing: UITableViewCell.self))
}
override func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: className, for: indexPath)
let className = rows[(indexPath as NSIndexPath).row] as! String
//Addtional configuration for the cell
switch className {
case String(describing: UITableViewCell.self):
cell.textLabel?.text = "A tottally native cell"
break
default:
break
}
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: IndexPath) -> CGFloat {
return 44.0
}
3. 创建 ModulesViewController
你需要创建一个 ModulesViewController
的子类
class MyViewController: ModulesViewController {}
4. 在 ViewController 中重写必要的函数
在 ModulesViewController
中需要重写的函数比在 Modules
中少。通常只需要重写 createModules
函数。
override func createModules()
这个函数将为 view controller 可以拥有的所有模块添加所有模块。例如,如下所示
override func createModules() {
super.createModules()
appendModule(FirstSectionModule(tableView: tableView!))
}
ModulesViewController
带有函数的示例
import ModuleServices
class MyViewController: ModulesViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView?.rowHeight = UITableViewAutomaticDimension
tableView?.estimatedRowHeight = 44
tableView?.tableFooterView = UIView()
}
override func createModules() {
super.createModules()
appendModule(FirstSectionModule(tableView: tableView!))
}
}
如你所见,这是一种非常好的避免大型 view controller 的方法。主要思想是分割 responsibility。
- 一个
ModulesViewController
将会管理(添加/删除)TableSectionModule
TableSectionModule
将会管理该 section 将包含的 cellsTableSectionModule
可以包含足够的逻辑和 responsibility 来使这一部分的UITableView
完全工作
作者
Francisco Javier Trujillo Mata, [email protected]
许可证
ModuleServices 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。