From 221efc00abc4779fc5aa0f11ec89abef3c681c1e Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Sat, 18 Jun 2016 05:42:49 +0300 Subject: [PATCH] add register cell method --- README.md | 1 + Sources/ConfigurableCell.swift | 20 ++++++++++++++++---- Sources/TableDirector.swift | 15 +++++++++++++++ Tests/TableKitTests.swift | 1 + 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a86fc2c..4f053bd 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/Sources/ConfigurableCell.swift b/Sources/ConfigurableCell.swift index 3e42f17..473f7cc 100644 --- a/Sources/ConfigurableCell.swift +++ b/Sources/ConfigurableCell.swift @@ -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 diff --git a/Sources/TableDirector.swift b/Sources/TableDirector.swift index 72b8fb1..3d67049 100644 --- a/Sources/TableDirector.swift +++ b/Sources/TableDirector.swift @@ -57,6 +57,21 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate public func reload() { tableView?.reloadData() } + + public func register(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 diff --git a/Tests/TableKitTests.swift b/Tests/TableKitTests.swift index 2a26050..bb2d8f8 100644 --- a/Tests/TableKitTests.swift +++ b/Tests/TableKitTests.swift @@ -28,6 +28,7 @@ class TestController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() tableDirector = TableDirector(tableView: tableView) + tableDirector.register([TestTableViewCell.self]) } }