From 721ba1972a9813021dadc908b1de6a29bd5f19bd Mon Sep 17 00:00:00 2001 From: Alexey Shpirko Date: Tue, 28 Feb 2017 22:27:59 +0300 Subject: [PATCH] added replca method to director --- Sources/TableDirector.swift | 9 +++++++ Tests/TableKitTests.swift | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 33846c0..808d4be 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -274,6 +274,15 @@ extension TableDirector { return self } + @discardableResult + open func replaceSection(at index: Int, with section: TableSection) -> Self { + + if index < sections.count { + sections[index] = section + } + return self + } + @discardableResult open func delete(sectionAt index: Int) -> Self { diff --git a/Tests/TableKitTests.swift b/Tests/TableKitTests.swift index 1ac4b01..5c1725f 100644 --- a/Tests/TableKitTests.swift +++ b/Tests/TableKitTests.swift @@ -190,4 +190,54 @@ class TabletTests: XCTestCase { waitForExpectations(timeout: 1.0, handler: nil) } + + func testReplaceSectionOnExistingIndex() { + + let row1 = TableRow(item: TestData(title: "title1")) + let row2 = TableRow(item: TestData(title: "title2")) + + let section1 = TableSection(headerView: nil, footerView: nil, rows: nil) + section1 += row1 + + let section2 = TableSection(headerView: nil, footerView: nil, rows: nil) + section2 += row2 + + testController.tableDirector += section1 + testController.tableView.reloadData() + + let cell = testController.tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as? TestTableViewCell + XCTAssertTrue(cell?.textLabel?.text == "title1") + + testController.tableDirector.replaceSection(at: 0, with: section2) + testController.tableView.reloadData() + + let cell1 = testController.tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as? TestTableViewCell + XCTAssertTrue(cell1?.textLabel?.text == "title2") + } + + + func testReplaceSectionOnWrongIndex() { + + let row1 = TableRow(item: TestData(title: "title1")) + let row2 = TableRow(item: TestData(title: "title2")) + + let section1 = TableSection(headerView: nil, footerView: nil, rows: nil) + section1 += row1 + + let section2 = TableSection(headerView: nil, footerView: nil, rows: nil) + section2 += row2 + + testController.tableDirector += section1 + testController.tableView.reloadData() + + let cell = testController.tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as? TestTableViewCell + XCTAssertTrue(cell?.textLabel?.text == "title1") + + testController.tableDirector.replaceSection(at: 33, with: section2) + testController.tableView.reloadData() + + let cell1 = testController.tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as? TestTableViewCell + XCTAssertTrue(cell1?.textLabel?.text == "title1") + } + }