-
-
Save embassem/08f04d1621db7fdd1bdc1f14c8fe3945 to your computer and use it in GitHub Desktop.
Revisions
-
JaviSoto revised this gist
May 16, 2016 . 1 changed file with 31 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ import UIKit import Rswift struct NibResource: NibResourceType { let name: String let bundle: NSBundle init(name: String, bundle: NSBundle = NSBundle.mainBundle()) { self.name = name self.bundle = bundle } } protocol NibInstantiable: class { associatedtype NibResourceConcreteType: NibResourceType static var nibResource: NibResourceConcreteType { get } } extension NibInstantiable { static var nib: UINib { return UINib( nibName: self.nibResource.name, bundle: self.nibResource.bundle ) } static func instantiateFromNib() -> Self { return self.nib.instantiateWithOwner(nil, options: nil).first! as! Self } } -
JaviSoto revised this gist
May 16, 2016 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,16 @@ final class SampleTableViewCell: UITableViewCell, ReusableNibTableViewCell { static let nibResource = R.nib.sampleTableViewCell } final class SampleViewController: UIViewController, UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad() self.tableView.registerReusableNibCell(SampleTableViewCell) } @objc func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { return SampleTableViewCell.dequeueFromTableView(tableView, indexPath) { cell in /// configure `cell` } } -
JaviSoto created this gist
May 16, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ import Foundation import Rswift protocol ReusableNibTableViewCell: NibInstantiable { static var reuseIdentifier: IdentifierType { get } } extension ReusableNibTableViewCell where NibResourceConcreteType: ReuseIdentifierType, NibResourceConcreteType.ReusableType == Self { /// Default implementation. Rswift's cell nib structs conform to `ReuseIdentifierType` with an associated type that matches Self /// So we can know at runtime that this reuse identifier matches this cell, and cells can conform to `ReusableNibTableViewCell` just by /// conforming to `NibInstantiable` static var reuseIdentifier: IdentifierType { return self.nibResource } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ import UIKit import Rswift extension UITableView { func registerReusableNibCell<C: ReusableNibTableViewCell where C: UITableViewCell>(_: C.Type) { self.registerNib(C.nib, forCellReuseIdentifier: C.reuseIdentifier.identifier) } } extension ReusableNibTableViewCell where Self: UITableViewCell { static func dequeueFromTableView(tableView: UITableView, _ indexPath: NSIndexPath, @noescape configure: (Self -> ()) = { _ in }) -> Self { let cell = tableView.dequeueReusableCellWithIdentifier(Self.reuseIdentifier.identifier, forIndexPath: indexPath) as! Self configure(cell) return cell } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ final class SampleTableViewCel: UITableViewCell, ReusableNibTableViewCell { static let nibResource = R.nib.sampleTableViewCell } final class SampleViewController: UIViewController, UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad() self.tableView.registerReusableNibCell(SimpleTitleBasedTableViewCell) } @objc func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { return SampleTableViewCel.dequeueFromTableView(tableView, indexPath) { cell in /// configure `cell` } } }