FTMTableSectionModules
示例
要运行示例项目,请首先克隆仓库,然后从示例目录运行pod install
。
要求
适用于iOS 7及以上版本。需要Swift 5.0和XCode 8.0或更高版本。
安装
FTMTableSectionModules可通过CocoaPods使用。要安装它,只需将以下行添加到您的Podfile中
pod 'FTMTableSectionModules'
如何使用它
FTMTableSectionModules实质上是一套工具包,帮助您更快地开发。这个库对基于UITableView的UIViewControllers非常有用。这个库的主要概念是,模块代表UITableView中的一个Section,所以有一个名为ModulesViewController的UIViewController子类,用于管理所有模块。
模块类似于一个迷你UIViewController,应该能够自我运行。
基本上,ModulesViewController有一个TableSectionModules数组。
有时候,一个例子比1000个字更容易理解,所以,请查看例子使用。
pod try 'FTMTableSectionModules'
如果您没有安装pod try
,请访问https://github.com/CocoaPods/cocoapods-try来安装它。
在里面,您可以看到一个带有两个不同模块和每个模块几个单元的ViewController的例子。
如果仍然想在这里查看示例,让我们试一试。
1. 创建一个模块
您需要创建一个TableSectionModule
的子类。
class FirstSectionModule: TableSectionModule {}
2. 覆盖模块中需要的方法
有非常多可以覆写的方法,最常见的是
- 使用
Class
/注册 UITableViewCell
/UITableViewHeaderFooterView
override func registerClassForCells() -> [AnyClass]
override func registerClassForHeadersFooters() -> [AnyClass]
override func registerNibsForCells() -> [AnyClass]
override func registerNibsForHeadersFooters() -> [AnyClass]
- 创建行,这是类似于
UITableView
的数据源
override func createRows()
UITableViewCell
的 deque 和配置
override func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell
-
其他可以覆写的方法您基本上可以覆写和
UITableViewDelegate
和UITableViewDataSource
提供相同的方法。 -
显然,您需要创建&配置模块中包含的所有
UITableViewCells
。
带有方法的TableSectionModule示例
import FTMTableSectionModules
class FirstSectionModule: TableSectionModule {
override func registerNibsForCells() -> [AnyClass] {
return super.registerNibsForCells() + [
Example1TableViewCell.classForCoder(),
]
}
override func registerClassForCells() -> [AnyClass] {
return 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()
该方法会将视图控制器可以拥有的所有模块添加进来。例如:
override func createModules() {
super.createModules()
appendModule(FirstSectionModule(tableView: tableView!))
}
ModulesViewController
方法的示例
import FTMTableSectionModules
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!))
}
}
如你所见,这是一种非常好的方法来避免创建大量视图控制器。主要的思想是分解职责。
ModulesViewController
将负责管理(添加/移除)TableSectionModule
TableSectionModule
将管理本节包含的单元格TableSectionModule
可以包含足够多的逻辑和职责,以使该部分UITableView
完全运行
作者
Francisco Javier Trujillo Mata, [email protected]
许可证
FTMTableSectionModules遵循MIT许可证。更多信息请参阅LICENSE文件。