From 856e476ca755e24b25924790aa82df9805b8fd6a Mon Sep 17 00:00:00 2001 From: Matias Rojas Date: Sat, 12 Sep 2020 13:20:36 -0300 Subject: [PATCH 1/2] Adding the ability to provide section header and footer dynamically --- Sources/TableDirector.swift | 4 ++-- Sources/TableSection.swift | 5 ++++- Tests/TableKitTests.swift | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 2ffc99d..b6040f4 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -220,13 +220,13 @@ 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?(section) } 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?(section) } open func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { diff --git a/Sources/TableSection.swift b/Sources/TableSection.swift index d1a3e8a..29a10d2 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: ((Int) -> UIView?)? + open var footerViewHandler: ((Int) -> 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..fda49af 100644 --- a/Tests/TableKitTests.swift +++ b/Tests/TableKitTests.swift @@ -168,6 +168,32 @@ 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 = { sectionNumber in + return sectionHeaderView + } + section.footerViewHandler = { sectionNumber in + return 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() { From 22614f65435ba357e8c7347c194c80c04f7d321a Mon Sep 17 00:00:00 2001 From: Matias Rojas Date: Sat, 12 Sep 2020 17:41:29 -0300 Subject: [PATCH 2/2] Adding the ability to provide section header and footer dynamically (fix for header/footer height) --- Sources/TableDirector.swift | 10 +++++++--- Sources/TableSection.swift | 4 ++-- Tests/TableKitTests.swift | 8 ++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index b6040f4..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 ?? sections[section].headerViewHandler?(section) + 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 ?? sections[section].footerViewHandler?(section) + 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 29a10d2..9755ec4 100644 --- a/Sources/TableSection.swift +++ b/Sources/TableSection.swift @@ -31,8 +31,8 @@ open class TableSection { open var headerView: UIView? open var footerView: UIView? - open var headerViewHandler: ((Int) -> UIView?)? - open var footerViewHandler: ((Int) -> UIView?)? + open var headerViewHandler: (() -> UIView?)? + open var footerViewHandler: (() -> UIView?)? open var headerHeight: CGFloat? = nil open var footerHeight: CGFloat? = nil diff --git a/Tests/TableKitTests.swift b/Tests/TableKitTests.swift index fda49af..522141d 100644 --- a/Tests/TableKitTests.swift +++ b/Tests/TableKitTests.swift @@ -177,12 +177,8 @@ class TableKitTests: XCTestCase { let sectionFooterView = UIView() let section = TableSection(rows: nil) - section.headerViewHandler = { sectionNumber in - return sectionHeaderView - } - section.footerViewHandler = { sectionNumber in - return sectionFooterView - } + section.headerViewHandler = { sectionHeaderView} + section.footerViewHandler = { sectionFooterView } section += row testController.tableDirector += section