CollapsibleTableSectionViewController
Swift 库,用于支持表格视图中的可折叠部分。
特点
- 支持表格视图中的可折叠部分
- 默认折叠所有部分(可配置)
- 同时只展开一个部分(可配置)
- 自动调整表格单元格大小
- 易于使用的配置协议
需求
- iOS 9.0+
- Xcode 10.0+
- Swift 4.2
安装
手册
只须复制以下 Swift 文件到您的项目中
- CollapsibleTableSectionViewController.swift
- CollapsibleTableViewHeader.swift
Cocoapods
- 请确保使用最新的稳定版 Cocoapods 版本:
pod --version
- 如果不是,请更新它:
sudo gem install cocoapods
- 在项目根目录下执行
pod init
- 打开
use_frameworks!
pod 'CollapsibleTableSectionViewController', '~> 1.0.0'
- 保存文件:
ctrl-x
,y
,enter
运行
pod update
- 打开生成的
.xcworkspace
- 别忘了导入 CollapsibleTableSectionViewController:
import CollapsibleTableSectionViewController
!
Carthage
使用
nano Cartfile
- 在 Cartfile 中添加
github "jeantimex/CollapsibleTableSectionViewController" ~> 1.0.0
- 保存文件:
ctrl-x
,y
,enter
- 运行
carthage update
- 将
CollapsibleTableSectionViewController.framework
从Carthage/Build/iOS
拷贝到您的项目中 - 确保将
CollapsibleTableSectionViewController
添加到您的目标的Embedded Binaries
部分中(否则,您将会遇到dyld library not loaded referenced from ... reason image not found
错误) - 在您的视图控制器的代码顶部添加
import CollapsibleTableSectionViewController
使用方法
CollapsibleTableSectionViewController
的子类
第1步。创建 import CollapsibleTableSectionViewController
class ViewController: CollapsibleTableSectionViewController { ... }
CollapsibleTableSectionDelegate
协议
步骤 2. 遵守 extension ViewController: CollapsibleTableSectionDelegate { ... }
CollapsibleTableSectionDelegate 协议
大多数协议方法是可选的,并且与 UITableViewDataSource
和 UITableViewDelegate
非常相似,以下是可用的协议方法列表
1. 可选函数 func numberOfSections(_ tableView: UITableView) -> Int
询问数据源返回表格视图中的节数。默认值为 1
。
extension ViewController: CollapsibleTableSectionDelegate {
func numberOfSections(_ tableView: UITableView) -> Int {
return 10
}
}
2. 可选函数 func collapsibleTableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
返回指定节中的行(表格单元格)数。默认值为 0
。
extension ViewController: CollapsibleTableSectionDelegate {
func collapsibleTableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
}
3. 可选函数 func collapsibleTableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
返回指定索引路径的表格单元格。您也可以使用自定义单元格,详情请见我们的示例项目。
extension ViewController: CollapsibleTableSectionDelegate {
func collapsibleTableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as UITableViewCell? ?? UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell.textLabel?.text = "Cell Text"
return cell
}
}
4. 可选函数 shouldCollapseByDefault(_ tableView: UITableView) -> Bool
当你想要在表加载时折叠所有部分时,返回 true
。默认值为 false
。
5. 可选函数 shouldCollapseOthers(_ tableView: UITableView) -> Bool
如果你想保持只有一个展开的部分(类似于手风琴样式),返回 true
。默认值为 false
。
6. 可选函数 collapsibleTableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
每个部分的标题。默认值为 nil
。
7. 可选函数 collapsibleTableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
告诉代理指定的行现在已被选中。
示例
运行本存储库中的Examples项目,你可以找到以下帮助您快速启动的示例
- 基本:最小的工作示例
- 自定义Cell:以编程方式实现自定义Cell
- 默认折叠:所有部分默认折叠
- 折叠其他:保持一个扩展部分的手风琴式表视图
有关使用Swift实现可折叠表格部分的详细说明,请查看以下仓库以获取更多信息:https://github.com/jeantimex/ios-swift-collapsible-table-section。
贡献
欢迎任何想为项目做出贡献的人。
- 复制此仓库
- 进行你的更改
- 提交拉取请求
许可协议
MIT 许可协议
版权所有(c)2017 Yong Su @jeantimex
特此授予任何人免费获得并使用此软件及其相关文档文件(统称为“软件”)的权利,而不会受到限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许将软件提供给软件提供者以使软件执行上述操作,但前提是必须遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“现状”提供,不提供任何形式的保证,明示或暗示,包括但不限于适销性、适用于特定目的和无侵犯性的保证。在任何情况下,作者或版权持有人都不会对任何主张、损害或其他责任负责,无论这些责任是在合同、侵权或其他法律事项中产生的,无论这些责任是在软件中、使用软件时或其他与软件有关的操作中产生的。