Skip to content

Dequable allows you to write strongly typed let constants for your dequable UI components and limits the need for string based cell identifiers.

License

Notifications You must be signed in to change notification settings

carabina/Dequable

Repository files navigation

Dequable

Carthage Cocoapods Build Swift Platform: iOS Twitter: @nashytitz

Dequable allows you to write strongly typed let constants for your dequable UI components and limits the need for string based cell identifiers. If you don't use interface builder, then you will not need to declare any cell identifiers.

Usage

Declare conformance.

class TableViewCell: UITableViewCell, DequeableComponentIdentifiable {}

class TableView: UITableView, DequeableTableView {}

If you use interface builder then you will need to specify a cell identifier in your interface builder file. The cell identifier should be "Filename" + "ID". In this example it would be "TableViewCellID".

Now you can dequee strongly typed let constants.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell: TableViewCell = (tableView as! DequeableTableView).dequeue(indexPath)
  return cell
}

It is assumed your interface builder file is in the same bundle. So in this example, if we created an interface builder file for TableViewCell, we would include it in the same bundle as the TableViewCell.swift file.

If you are not using storyboard + prototype cells then register your cells.

class TableView: UITableView, DequeableTableView {
    
  override init(frame: CGRect, style: UITableViewStyle) {
    super.init(frame: frame, style: style)
    register(cellType: TableViewCell.self, hasNib: false)
  }

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }
  
}

You may want to use the awesome require framework to lock down your optionals.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let dequeableTableView: DequeableTableView = (tableView as? DequeableTableView).require(hint: "TableView must conform to DequeableTableView")
  let cell: TableViewCell = dequeableTableView.dequeue(indexPath)
  return cell
}

Installing

Carthage:

Add github "rob-nash/Dequable" to your Cartfile.

Cocoapods:

Add pod "Dequable" to your Podfile.

Donations.

If you like this and you want to buy me a drink, use bitcoin.

Bitcoin Image

Bitcoin Address: 15Gj4DBTzSujnJrfRZ6ivrR9kDnWXNPvNQ

About

Dequable allows you to write strongly typed let constants for your dequable UI components and limits the need for string based cell identifiers.

Resources

License

Stars

Watchers

Forks

Packages

No packages published