diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 3d67049..170e06d 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -61,15 +61,24 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate public func register(cells: T.Type...) { for cell in cells { + if let nib = cell.nib() { + tableView?.registerNib(nib, forCellReuseIdentifier: cell.reusableIdentifier()) + return + } else { - if let nib = NSBundle(forClass: cell).loadNibNamed(cell.reusableIdentifier(), owner: nil, options: nil).first as? UINib { - tableView?.registerNib(nib, forCellReuseIdentifier: cell.reusableIdentifier()) - } else { - tableView?.registerClass(cell, forCellReuseIdentifier: cell.reusableIdentifier()) + + let resource = cell.reusableIdentifier() + let bundle = NSBundle(forClass: cell) + + if let _ = bundle.pathForResource(resource, ofType: "nib") { + tableView?.registerNib(UINib(nibName: resource, bundle: bundle), forCellReuseIdentifier: cell.reusableIdentifier()) + return } } + + tableView?.registerClass(cell, forCellReuseIdentifier: cell.reusableIdentifier()) } } diff --git a/Tests/TableKitTests.swift b/Tests/TableKitTests.swift index bb2d8f8..a3e3ea3 100644 --- a/Tests/TableKitTests.swift +++ b/Tests/TableKitTests.swift @@ -28,7 +28,7 @@ class TestController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() tableDirector = TableDirector(tableView: tableView) - tableDirector.register([TestTableViewCell.self]) + tableDirector.register(TestTableViewCell.self) } } @@ -75,7 +75,7 @@ class TabletTests: XCTestCase { super.setUp() testController = TestController() - let _ = testController.view + testController.view.hidden = false } override func tearDown() { @@ -91,21 +91,47 @@ class TabletTests: XCTestCase { XCTAssertNotNil(testController.tableDirector.tableView, "TableDirector should have table view") } - func testRow() { + func testRowInSection() { let data = TestData(title: "title") let row = TableRow(item: data) testController.tableDirector += row + testController.tableView.reloadData() XCTAssertTrue(testController.tableView.dataSource?.numberOfSectionsInTableView?(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") let cell = testController.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0)) as? TestTableViewCell XCTAssertNotNil(cell) + XCTAssertTrue(cell?.textLabel?.text == data.title) } + func testManyRowsInSection() { + + let data = [TestData(title: "1"), TestData(title: "2"), TestData(title: "3")] + + let rows: [TableRow] = data.map { TableRow(item: $0) } + + testController.tableDirector += rows + testController.tableView.reloadData() + + XCTAssertTrue(testController.tableView.dataSource?.numberOfSectionsInTableView?(testController.tableView) == 1, "Table view should have a section") + XCTAssertTrue(testController.tableView.dataSource?.tableView(testController.tableView, numberOfRowsInSection: 0) == data.count, "Table view should have certain number of rows in a section") + + for (index, element) in data.enumerate() { + + let cell = testController.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0)) as? TestTableViewCell + XCTAssertNotNil(cell) + XCTAssertTrue(cell?.textLabel?.text == element.title) + } + } + + + + + /*func testSimpleRowBuilderCreatesRowsAndSection() { let source = ["1", "2", "3"]