Skip to content

Commit

Permalink
Allows a section's header and footer to be nil (#1927)
Browse files Browse the repository at this point in the history
Under the hood, a `Section`'s header and footer can both be `nil`, but the available initializers do not allow you to specify `nil` values for these unless you use the zero-argument init method.

This change is backwards-compatible, and should not affect any existing code.
  • Loading branch information
aaronbrethorst authored and mats-claassen committed Nov 1, 2019
1 parent bc6e1d0 commit 834b9d8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
12 changes: 4 additions & 8 deletions Example/Example/Controllers/MultivaluedExamples.swift
Expand Up @@ -83,8 +83,7 @@ class MultivaluedController: FormViewController {
+++

MultivaluedSection(multivaluedOptions: [.Insert, .Delete, .Reorder],
header: "Multivalued Push Selector example",
footer: "") {
header: "Multivalued Push Selector example") {
$0.tag = "push"
$0.multivaluedRowToInsertAt = { index in
return PushRow<String>{
Expand Down Expand Up @@ -117,8 +116,7 @@ class MultivaluedOnlyReorderController: FormViewController {
form +++

MultivaluedSection(multivaluedOptions: .Reorder,
header: "Reordering Selectors",
footer: "") {
header: "Reordering Selectors") {
$0 <<< PushRow<String> {
$0.title = "Tap to select ;).."
$0.options = ["Option 1", "Option 2", "Option 3"]
Expand All @@ -141,8 +139,7 @@ class MultivaluedOnlyReorderController: FormViewController {
+++
// Multivalued Section with inline rows - section set up to support only reordering
MultivaluedSection(multivaluedOptions: .Reorder,
header: "Reordering Inline Rows",
footer: "") { section in
header: "Reordering Inline Rows") { section in
list.enumerated().forEach({ offset, string in
let dateInlineRow = DateInlineRow(){
$0.value = Date(timeInterval: Double(-secondsPerDay) * Double(offset), since: Date())
Expand All @@ -155,8 +152,7 @@ class MultivaluedOnlyReorderController: FormViewController {
+++

MultivaluedSection(multivaluedOptions: .Reorder,
header: "Reordering Field Rows",
footer: "")
header: "Reordering Field Rows")
<<< NameRow {
$0.value = "Martin"
}
Expand Down
26 changes: 17 additions & 9 deletions Source/Core/Section.swift
Expand Up @@ -186,19 +186,27 @@ open class Section {
initializer(self)
}

public init(_ header: String, _ initializer: @escaping (Section) -> Void = { _ in }) {
self.header = HeaderFooterView(stringLiteral: header)
public init(_ header: String?, _ initializer: @escaping (Section) -> Void = { _ in }) {
if let header = header {
self.header = HeaderFooterView(stringLiteral: header)
}
initializer(self)
}

public init(header: String, footer: String, _ initializer: (Section) -> Void = { _ in }) {
self.header = HeaderFooterView(stringLiteral: header)
self.footer = HeaderFooterView(stringLiteral: footer)
public init(header: String?, footer: String?, _ initializer: (Section) -> Void = { _ in }) {
if let header = header {
self.header = HeaderFooterView(stringLiteral: header)
}
if let footer = footer {
self.footer = HeaderFooterView(stringLiteral: footer)
}
initializer(self)
}

public init(footer: String, _ initializer: (Section) -> Void = { _ in }) {
self.footer = HeaderFooterView(stringLiteral: footer)
public init(footer: String?, _ initializer: (Section) -> Void = { _ in }) {
if let footer = footer {
self.footer = HeaderFooterView(stringLiteral: footer)
}
initializer(self)
}

Expand Down Expand Up @@ -493,8 +501,8 @@ open class MultivaluedSection: Section {
public var multivaluedRowToInsertAt: ((Int) -> BaseRow)?

public required init(multivaluedOptions: MultivaluedOptions = MultivaluedOptions.Insert.union(.Delete),
header: String = "",
footer: String = "",
header: String? = nil,
footer: String? = nil,
_ initializer: (MultivaluedSection) -> Void = { _ in }) {
self.multivaluedOptions = multivaluedOptions
super.init(header: header, footer: footer, {section in initializer(section as! MultivaluedSection) })
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/SelectableSection.swift
Expand Up @@ -127,13 +127,13 @@ open class SelectableSection<Row>: Section, SelectableSectionType where Row: Sel
initializer(self)
}

public init(_ header: String, selectionType: SelectionType, _ initializer: @escaping (SelectableSection<Row>) -> Void = { _ in }) {
public init(_ header: String?, selectionType: SelectionType, _ initializer: @escaping (SelectableSection<Row>) -> Void = { _ in }) {
self.selectionType = selectionType
super.init(header, { _ in })
initializer(self)
}

public init(header: String, footer: String, selectionType: SelectionType, _ initializer: @escaping (SelectableSection<Row>) -> Void = { _ in }) {
public init(header: String?, footer: String?, selectionType: SelectionType, _ initializer: @escaping (SelectableSection<Row>) -> Void = { _ in }) {
self.selectionType = selectionType
super.init(header: header, footer: footer, { _ in })
initializer(self)
Expand Down
12 changes: 12 additions & 0 deletions Tests/MultivaluedSectionTests.swift
Expand Up @@ -44,6 +44,18 @@ class MultivaluedSectionTests: XCTestCase {
super.tearDown()
}

func testHeaders() {
let headerSection = MultivaluedSection(multivaluedOptions: .Insert, header: "Header Text", footer: nil) { _ in }
XCTAssertEqual(headerSection.header!.title, "Header Text")
XCTAssertNil(headerSection.footer)
}

func testFooters() {
let footerSection = MultivaluedSection(multivaluedOptions: .Insert, header: nil, footer: "Footer Text") { _ in }
XCTAssertEqual(footerSection.footer!.title, "Footer Text")
XCTAssertNil(footerSection.header)
}

func testAddButton() {
let section = MultivaluedSection(multivaluedOptions: .Insert, header: "", footer: "") { _ in
// just an empty closure
Expand Down

0 comments on commit 834b9d8

Please sign in to comment.