diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 2ffc99d..8813c17 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -220,20 +220,23 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { open func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { guard section < sections.count else { return nil } - return sections[section].headerView + return sections[section].headerView ?? sections[section].headerViewHandler?() } open func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { guard section < sections.count else { return nil } - return sections[section].footerView + return sections[section].footerView ?? sections[section].footerViewHandler?() } open func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { guard section < sections.count else { return 0 } let section = sections[section] - return section.headerHeight ?? section.headerView?.frame.size.height ?? UITableView.automaticDimension + return section.headerHeight + ?? section.headerView?.frame.size.height + ?? section.headerViewHandler?()?.frame.size.height + ?? UITableView.automaticDimension } open func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { @@ -242,6 +245,7 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate { let section = sections[section] return section.footerHeight ?? section.footerView?.frame.size.height + ?? section.footerViewHandler?()?.frame.size.height ?? UITableView.automaticDimension } diff --git a/Sources/TableSection.swift b/Sources/TableSection.swift index d1a3e8a..9755ec4 100644 --- a/Sources/TableSection.swift +++ b/Sources/TableSection.swift @@ -30,6 +30,9 @@ open class TableSection { open var headerView: UIView? open var footerView: UIView? + + open var headerViewHandler: (() -> UIView?)? + open var footerViewHandler: (() -> UIView?)? open var headerHeight: CGFloat? = nil open var footerHeight: CGFloat? = nil @@ -62,7 +65,7 @@ open class TableSection { self.headerView = headerView self.footerView = footerView } - + // MARK: - Public - open func clear() { diff --git a/Tests/TableKitTests.swift b/Tests/TableKitTests.swift index d9d5b11..522141d 100644 --- a/Tests/TableKitTests.swift +++ b/Tests/TableKitTests.swift @@ -168,6 +168,28 @@ class TableKitTests: XCTestCase { XCTAssertTrue(testController.tableView.delegate?.tableView?(testController.tableView, viewForHeaderInSection: 0) == sectionHeaderView) XCTAssertTrue(testController.tableView.delegate?.tableView?(testController.tableView, viewForFooterInSection: 0) == sectionFooterView) } + + func testTableSectionCreatesSectionWithHeaderAndFooterViewsCallbacks() { + + let row = TableRow(item: TestData(title: "title")) + + let sectionHeaderView = UIView() + let sectionFooterView = UIView() + + let section = TableSection(rows: nil) + section.headerViewHandler = { sectionHeaderView} + section.footerViewHandler = { sectionFooterView } + section += row + + testController.tableDirector += section + testController.tableView.reloadData() + + XCTAssertTrue(testController.tableView.dataSource?.numberOfSections?(in: testController.tableView) == 1, "Table view should have a section") + XCTAssertTrue(testController.tableView.dataSource?.tableView(testController.tableView, numberOfRowsInSection: 0) == 1, "Table view should have certain number of rows in a section") + + XCTAssertTrue(testController.tableView.delegate?.tableView?(testController.tableView, viewForHeaderInSection: 0) == sectionHeaderView) + XCTAssertTrue(testController.tableView.delegate?.tableView?(testController.tableView, viewForFooterInSection: 0) == sectionFooterView) + } func testRowBuilderCustomActionInvokedAndSentUserInfo() {