fix register cells

This commit is contained in:
Max Sokolov 2016-06-18 06:03:29 +03:00
parent 221efc00ab
commit dd6092f87f
2 changed files with 42 additions and 7 deletions

View File

@ -61,15 +61,24 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
public func register<T: UITableViewCell where T: ReusableCell>(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())
}
}

View File

@ -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<TestData, TestTableViewCell>(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<TestData, TestTableViewCell>] = data.map { TableRow<TestData, TestTableViewCell>(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"]