add register cell method

This commit is contained in:
Max Sokolov 2016-06-18 05:42:49 +03:00
parent 23cceddfe2
commit 221efc00ab
4 changed files with 33 additions and 4 deletions

View File

@ -44,6 +44,7 @@ let section = TableSection(rows: [row1, row2, row3])
And setup your table:
```swift
let tableDirector = TableDirector(tableView: tableView)
tableDirector.register(StringTableViewCell.self, IntTableViewCell.self, FloatTableViewCell.self)
tableDirector += section
```
Done. Your table is ready. You may want to look at your cell. It has to conform to `ConfigurableCell` protocol:

View File

@ -20,21 +20,33 @@
import UIKit
public protocol ConfigurableCell {
public protocol ReusableCell {
static func reusableIdentifier() -> String
static func nib() -> UINib?
}
public protocol ConfigurableCell: ReusableCell {
associatedtype T
static func reusableIdentifier() -> String
static func estimatedHeight() -> CGFloat
static func defaultHeight() -> CGFloat?
func configure(_: T, isPrototype: Bool)
}
public extension ConfigurableCell where Self: UITableViewCell {
public extension ReusableCell where Self: UITableViewCell {
static func reusableIdentifier() -> String {
return String(self)
}
static func nib() -> UINib? {
return nil
}
}
public extension ConfigurableCell where Self: UITableViewCell {
static func estimatedHeight() -> CGFloat {
return UITableViewAutomaticDimension

View File

@ -57,6 +57,21 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
public func reload() {
tableView?.reloadData()
}
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())
} 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())
}
}
}
}
// MARK: Public

View File

@ -28,6 +28,7 @@ class TestController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableDirector = TableDirector(tableView: tableView)
tableDirector.register([TestTableViewCell.self])
}
}