Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic section header and footer views #99

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Sources/TableDirector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
5 changes: 4 additions & 1 deletion Sources/TableSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,7 +65,7 @@ open class TableSection {
self.headerView = headerView
self.footerView = footerView
}

// MARK: - Public -

open func clear() {
Expand Down
22 changes: 22 additions & 0 deletions Tests/TableKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestTableViewCell>(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() {

Expand Down