ExpandableCell
介绍
对YNExapnadableCell 进行了完全重构,更加简洁、无bug。iOS 中Expandable & collapsible cell的最简单用法,用Swift 5 编写。你可以自定义你喜欢的expandable UITableViewCell
。创建 ExpandableCell
是因为 insertRows
和 deleteRows
使用困难。只需继承 ExpandableDelegate
用法
基础用法
import ExpandableCell
在 Storyboard 或在代码中创建 ExpandableTableView
@IBOutlet var tableView: ExpandableTableView!
继承 ExpandableDelegate
class ViewController: UIViewController, ExpandableDelegate
设置委托
tableView.expandableDelegate = self
设置所需的 ExpandableDelegate
方法
两种关键方法
必需的 ExpandableDelegate | 说明 |
---|---|
func expandableTableView(_ expandableTableView: ExpandableTableView, expandedCellsForRowAt indexPath: IndexPath) -> [UITableViewCell]? |
获取可展开单元格的关键方法 |
func expandableTableView(_ expandableTableView: ExpandableTableView, heightsForExpandedRowAt indexPath: IndexPath) -> [CGFloat]? |
获取可展开单元格高度的关键方法 |
必需的 UITableViewDelegate, UITableViewDataSource | 说明 |
---|---|
func expandableTableView(_ expandableTableView: ExpandableTableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell |
- |
func expandableTableView(_ expandableTableView: ExpandableTableView, numberOfRowsInSection section: Int) -> Int |
- |
func expandableTableView(_ expandableTableView: ExpandableTableView, heightForRowAt indexPath: IndexPath) -> CGFloat |
- |
高级
ExpandableTableView 属性
属性 | 类型 | 说明 |
---|---|---|
animation |
UITableViewRowAnimation |
打开和关闭时的动画 |
expansionStyle |
ExpandableTableView.ExpansionStyle |
选择展开类型 single - 每次一行; singlePerSection - 每个分区每次一行; multi - 每次任意数量的行 |
autoRemoveSelection |
Bool |
autoRemoveSelection true 意味着单元格将闪烁选中,autoRemoveSelection false 意味着将应用 tableview 的默认选择行为(单选或多选) |
ExpandableTableView 方法
方法 | 说明 |
---|---|
openAll |
打开在 func expandableTableView(_ expandableTableView: ExpandableTableView, expandedCellsForRowAt indexPath: IndexPath) -> [UITableViewCell]? 中设置的所有的 |
closeAll |
关闭在 func expandableTableView(_ expandableTableView: ExpandableTableView, expandedCellsForRowAt indexPath: IndexPath) -> [UITableViewCell]? 中设置的所有的 |
reloadData |
TableView 重新加载数据。展开的单元格也将工作 |
open(at indexPath: IndexPath) |
打开特定的 indexPath |
可选委托
可选的可展开Delegate | 说明 |
---|---|
func expandableTableView(_ expandableTableView: ExpandableTableView, didSelectExpandedRowAt indexPath: IndexPath) |
获取展开行的索引路径 |
func expandableTableView(_ expandableTableView: ExpandableTableView, expandedCell: UITableViewCell, didSelectExpandedRowAt indexPath: IndexPath) |
获取展开的单元格和索引路径 |
可选的UITableViewDelegate, UITableViewDataSource | 说明 |
---|---|
func expandableTableView(_ expandableTableView: ExpandableTableView, didSelectRowAt indexPath: IndexPath) |
- |
func expandableTableView(_ expandableTableView: ExpandableTableView, titleForHeaderInSection section: Int) -> String? |
- |
func expandableTableView(_ expandableTableView: ExpandableTableView, heightForHeaderInSection section: Int) -> CGFloat |
- |
func expandableTableView(_ expandableTableView: ExpandableTableView, viewForHeaderInSection section: Int) -> UIView? |
- |
func numberOfSections(in expandableTableView: ExpandableTableView) -> Int |
- |
func expandableTableView(_ expandableTableView: ExpandableTableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) |
- |
func expandableTableView(_ expandableTableView: ExpandableTableView, willDisplayHeaderView view: UIView, forSection section: Int) |
- |
func expandableTableView(_ expandableTableView: ExpandableTableView, willDisplayFooterView view: UIView, forSection section: Int) |
- |
用于箭头效果
当你需要箭头效果或更改箭头图标时,继承ExpandableCell
open class ExpandableCell: UITableViewCell {
open var arrowImageView: UIImageView!
}
用于高亮动画
当你需要禁用或启用高亮动画时,继承ExpandableCell
open class ExpandableCell: UITableViewCell {
open var highlightAnimation = HighlightAnimation.animated
}
向箭头图标添加右边距
当你需要右边距(默认边距为16)时,继承ExpandableCell
open class ExpandableCell: UITableViewCell {
open var rightMargin: CGFloat = 16
}
设置tableView插入动画
tableView.animation = .automatic
根据需要或在ExpandableDelegate
中创建协议,或者向我提交Pull Request :)
ExpandableCell 方法
ExpandableCell 方法 | 说明 |
---|---|
isExpanded() |
检查单元格是否已展开 |
isInitiallyExpanded() |
确保单元格在表格视图内容首次出现在视图时是打开的 |
isSelectable() |
使单元格可选中或不可选,无论表格视图选择样式如何 |
要求
ExpandableCell
采用 Swift 5.0 开发。与 iOS 8.0+ 兼容。
安装
Cocoapods
ExpandableCell 可通过 CocoaPods 使用。要安装它,只需将以下行添加到 Podfile 中
pod 'ExpandableCell'
Carthage
github "younatics/ExpandableCell"
参考
请告知我或在您的应用中使用此库时创建拉取请求:)
作者
许可证
ExpandableCell 根据MIT许可证发行。有关更多信息,请参阅LICENSE文件。