
- Using Protocol to set, the expand/collapse function can be easily realized without affecting the native UITableViewCell function.
- 使用 Protocol 来设置,在不影响原生的 UITableViewCell 功能之下,能简单地实现展开 / 折叠的功能。
Achievements display - 成果展示

dependencies: [
.package(url: "https://github.com/William-Weng/WWExpandableCell.git", .upToNextMajor(from: "1.0.0"))
]
- The part to be collapsed, use WWExpandView type.
- 将要折叠的部分,使用 WWExpandView 类型。

函式 |
功能 |
expandedCells(section:rowCount:) |
设定预设值 => 那个 section 的 cell 们预设是打开的 |
expandedCell(section:row:) |
设定预设值 => 那一个 IndexPath 是打开的 |
exchangeExpandState(_:indexPath:isSingle:duration:delay:options:) |
交换展开状态 |
import UIKit
final class TableViewDemoController: UIViewController {
@IBOutlet weak var myTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
initSetting()
}
deinit {
MyTableViewCell.myTableView = nil
}
}
// MARK: - UITableViewDelegate, UITableViewDataSource
extension TableViewDemoController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int { return 3 }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { return cellMaker(tableView, cellForRowAt: indexPath)! }
}
// MARK: - 小工具
private extension TableViewDemoController {
/// 初始化設定
func initSetting() {
myTableView.delegate = self
myTableView.dataSource = self
MyTableViewCell.myTableView = myTableView
MyTableViewCell.expandedCell(section: 0, row: 0)
}
/// Cell產生器
/// - Parameters:
/// - tableView: UITableView
/// - indexPath: IndexPath
/// - Returns: MyTableViewCell
func cellMaker(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> MyTableViewCell? {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell", for: indexPath) as? MyTableViewCell
cell?.configure(with: indexPath)
return cell
}
}
import UIKit
import WWExpandableCell
final class MyTableViewCell: UITableViewCell {
@IBOutlet weak var myExpandView: WWExpandView!
@IBOutlet weak var myLabel: UILabel!
static weak var myTableView: UITableView?
static var expandRowsList: [Int: Set<IndexPath>] = [:]
var indexPath: IndexPath = []
private let isSingle = true
/// 展開 / 折疊
/// - Parameter sender: UIButton
@IBAction func expandAction(_ sender: UIButton) {
guard let myTableView = Self.myTableView else { return }
MyTableViewCell.exchangeExpandState(myTableView, indexPath: indexPath, isSingle: isSingle)
}
}
// MARK: - WWCellExpandable
extension MyTableViewCell: WWCellExpandable {
func configure(with indexPath: IndexPath) {
self.indexPath = indexPath
myExpandView.isHidden = !(Self.expandRowsList[indexPath.section]?.contains(indexPath) ?? false)
myLabel.text = "\(indexPath)"
}
func expandView() -> WWExpandView? { return myExpandView }
}