Skip to content

Instantly share code, notes, and snippets.

@embassem
Forked from JaviSoto/NibInstantiable.swift
Created April 14, 2020 11:55
Show Gist options
  • Select an option

  • Save embassem/08f04d1621db7fdd1bdc1f14c8fe3945 to your computer and use it in GitHub Desktop.

Select an option

Save embassem/08f04d1621db7fdd1bdc1f14c8fe3945 to your computer and use it in GitHub Desktop.

Revisions

  1. @JaviSoto JaviSoto revised this gist May 16, 2016. 1 changed file with 31 additions and 0 deletions.
    31 changes: 31 additions & 0 deletions NibInstantiable.swift
    Original 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
    }
    }
  2. @JaviSoto JaviSoto revised this gist May 16, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions SampleTableViewCell.swift
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,16 @@
    final class SampleTableViewCel: UITableViewCell, ReusableNibTableViewCell {
    final class SampleTableViewCell: UITableViewCell, ReusableNibTableViewCell {
    static let nibResource = R.nib.sampleTableViewCell
    }

    final class SampleViewController: UIViewController, UITableViewDataSource {
    override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.registerReusableNibCell(SimpleTitleBasedTableViewCell)
    self.tableView.registerReusableNibCell(SampleTableViewCell)
    }

    @objc func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return SampleTableViewCel.dequeueFromTableView(tableView, indexPath) { cell in
    return SampleTableViewCell.dequeueFromTableView(tableView, indexPath) { cell in
    /// configure `cell`
    }
    }
  3. @JaviSoto JaviSoto created this gist May 16, 2016.
    15 changes: 15 additions & 0 deletions ReusableNibTableViewCell.swift
    Original 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
    }
    }
    18 changes: 18 additions & 0 deletions ReusableTableViewCell.swift
    Original 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
    }
    }
    17 changes: 17 additions & 0 deletions SampleTableViewCell.swift
    Original 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`
    }
    }
    }