<commit_before># Reusable

![Reusable](Example/ReusableDemo/Assets.xcassets/AppIcon.appiconset/AppIcon-167.png)

A Swift mixin to use `UITableViewCells` and `UICollectionViewCells` in a **type-safe way**, without the need to manipulate their `String`-typed `reuseIdentifiers`. This library also supports arbitrary `UIView` to be loaded via a XIB using a simple call to `loadFromNib()`

[![Platform](http://cocoapod-badges.herokuapp.com/p/Reusable/badge.png)](http://cocoadocs.org/docsets/Reusable)
[![Version](http://cocoapod-badges.herokuapp.com/v/Reusable/badge.png)](http://cocoadocs.org/docsets/Reusable)

*TL;DR:*

* Mark your `UITableViewCell` and `UICollectionViewCell` classes to conform to either `Reusable` or `NibReusable` (no additional code to implement!)
* Then simply use `tableView.dequeueReusableCell(indexPath: indexPath) as MyCustomCell` and you'll get a dequeued instance of the expected cell class in return. **No need for you to manipulate `reuseIdentifiers` manually!**

No more force-casting the returned `UITableViewCell` instance down to your `MyCustomCell` class, and no more fear that you'll mismatch the `reuseIdentifier` and the class you down-cast to. Now all you have is **a beautiful code and type-safe cells**!

> For more information on how this works, see [my dedicated blog post about this technique](http://alisoftware.github.io/swift/generics/2016/01/06/generic-tableviewcells/).

Note: the `Reusable` library can also be used to mark any arbitrary `UIView` as `NibLoadable` and then simply create an instance of that XIB-based view using `MyCustomView.loadFromNib()`.

## Declaring your cell subclasses

First, declare your cells to conform to:

* the `Reusable` protocol if they don't depend on a NIB (this will use `registerClass(…)` to register the cell)
* the `NibReusable` protocol if they use a `XIB` file for their content (this will use `registerNib(…)` to register the cell)

### Example with custom `UITableViewCell` subclasses

So for example to create a `UITableViewCell` subclass which doesn't use a XIB (either because it is only created via code, or because it will be registered automatically via a storyboard):

```swift
class CodeBasedCustomCell: UITableViewCell, Reusable {
  // By default this cell will have a reuseIdentifier of "MyCustomCell"
  // unless you provide an alternative implementation of `var reuseIdentifier`
  
  // No need to add anything to conform to Reusable. you can just keep your normal cell code
  @IBOutlet private weak var label: UILabel!
  func fillWithText(text: String?) { label.text = text }
}
```

And to create a `UITableViewCell` subclass whose content is based on a `XIB`:

```swift
class NibBasedCustomCell: UITableViewCell, NibReusable {
  // Here we provide a nib for this cell class (which, if we don't override the protocol's
  // default implementation of `nib`, will use a XIB of the same name as the class)
  
  // No need to add anything to conform to Reusable. you can just keep your normal cell code
  @IBOutlet private weak var pictureView: UIImageView!
  func fillWithImage(image: UIImage?) { pictureView.image = image }
}
```

> If you create a XIB-based cell, don't forget to set its _Reuse Identifier_ field in Interface Builder to the same string as the name of the cell class itself.

### Example with custom  `UICollectionViewCell` subclasses

This works exactly the same as `UITableViewCell`.

So for a Code-based `UICollectionViewCell` subclass:

```swift
// A UICollectionViewCell which doesn't need a XIB to register
// Either because it's all-code, or because it's registered via Storyboard
class CodeBasedCollectionViewCell: UICollectionViewCell, Reusable {
  // The rest of the cell code goes here
}
```

And for a XIB-based `UICollectionViewCell` subclass:

```swift
// A UICollectionViewCell using a XIB to define it's UI
// And that will need to register using that XIB
class NibBasedCollectionViewCell: UICollectionViewCell, NibReusable {
  // The rest of the cell code goes here
}
```

### Example with a XIB-based arbitrary UIView

`Reusable` can also be used to load an arbitrary `UIView` subclass (even a non-reusable, non-cell view) designed in a XIB by simply marking it as `NibLoadable`:

```swift
class NibBasedRandomView: UIView, NibLoadable {
  // The rest of the view code goes here
}
```

## Registering your cells by code

Then to use those cells, you'll register them like this, without the need to manipulate any `reuseIdentifier` anywhere in the code:

```swift
class MyViewController: UIViewController {
  @IBOutlet private weak var tableView: UITableView!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    tableView.registerReusableCell(CodeBasedCustomCell) // This will register using the class without using a UINib
    tableView.registerReusableCell(NibBasedCustomCell) // This will register using NibBasedCustomCell.xib
  }
}
```

> Note: If your cell is prototyped in a Storyboard, there is no need to call `registerReusableCell` for those cell prototypes, as the Storyboard auto-register these cell prototypes with the `UITableView` / `UICollectionView` they are prototyped into.

## Dequeuing your cells

Then in your implementation of `UIViewControllerDataSource`, especially to dequeue a cell you will be able to do:

```swift<commit_msg>* Change README (add NibOwnLoadable Example)<commit_after># Reusable

![Reusable](Example/ReusableDemo/Assets.xcassets/AppIcon.appiconset/AppIcon-167.png)

A Swift mixin to use `UITableViewCells` and `UICollectionViewCells` in a **type-safe way**, without the need to manipulate their `String`-typed `reuseIdentifiers`. This library also supports arbitrary `UIView` to be loaded via a XIB using a simple call to `loadFromNib()`

[![Platform](http://cocoapod-badges.herokuapp.com/p/Reusable/badge.png)](http://cocoadocs.org/docsets/Reusable)
[![Version](http://cocoapod-badges.herokuapp.com/v/Reusable/badge.png)](http://cocoadocs.org/docsets/Reusable)

*TL;DR:*

* Mark your `UITableViewCell` and `UICollectionViewCell` classes to conform to either `Reusable` or `NibReusable` (no additional code to implement!)
* Then simply use `tableView.dequeueReusableCell(indexPath: indexPath) as MyCustomCell` and you'll get a dequeued instance of the expected cell class in return. **No need for you to manipulate `reuseIdentifiers` manually!**

No more force-casting the returned `UITableViewCell` instance down to your `MyCustomCell` class, and no more fear that you'll mismatch the `reuseIdentifier` and the class you down-cast to. Now all you have is **a beautiful code and type-safe cells**!

> For more information on how this works, see [my dedicated blog post about this technique](http://alisoftware.github.io/swift/generics/2016/01/06/generic-tableviewcells/).

Note: the `Reusable` library can also be used to mark any arbitrary `UIView` as `NibLoadable` and then simply create an instance of that XIB-based view using `MyCustomView.loadFromNib()`.

## Declaring your cell subclasses

First, declare your cells to conform to:

* the `Reusable` protocol if they don't depend on a NIB (this will use `registerClass(…)` to register the cell)
* the `NibReusable` protocol if they use a `XIB` file for their content (this will use `registerNib(…)` to register the cell)

### Example with custom `UITableViewCell` subclasses

So for example to create a `UITableViewCell` subclass which doesn't use a XIB (either because it is only created via code, or because it will be registered automatically via a storyboard):

```swift
class CodeBasedCustomCell: UITableViewCell, Reusable {
  // By default this cell will have a reuseIdentifier of "MyCustomCell"
  // unless you provide an alternative implementation of `var reuseIdentifier`
  
  // No need to add anything to conform to Reusable. you can just keep your normal cell code
  @IBOutlet private weak var label: UILabel!
  func fillWithText(text: String?) { label.text = text }
}
```

And to create a `UITableViewCell` subclass whose content is based on a `XIB`:

```swift
class NibBasedCustomCell: UITableViewCell, NibReusable {
  // Here we provide a nib for this cell class (which, if we don't override the protocol's
  // default implementation of `nib`, will use a XIB of the same name as the class)
  
  // No need to add anything to conform to Reusable. you can just keep your normal cell code
  @IBOutlet private weak var pictureView: UIImageView!
  func fillWithImage(image: UIImage?) { pictureView.image = image }
}
```

> If you create a XIB-based cell, don't forget to set its _Reuse Identifier_ field in Interface Builder to the same string as the name of the cell class itself.

### Example with custom  `UICollectionViewCell` subclasses

This works exactly the same as `UITableViewCell`.

So for a Code-based `UICollectionViewCell` subclass:

```swift
// A UICollectionViewCell which doesn't need a XIB to register
// Either because it's all-code, or because it's registered via Storyboard
class CodeBasedCollectionViewCell: UICollectionViewCell, Reusable {
  // The rest of the cell code goes here
}
```

And for a XIB-based `UICollectionViewCell` subclass:

```swift
// A UICollectionViewCell using a XIB to define it's UI
// And that will need to register using that XIB
class NibBasedCollectionViewCell: UICollectionViewCell, NibReusable {
  // The rest of the cell code goes here
}
```

### Example with a XIB-based arbitrary UIView

`Reusable` can also be used to load an arbitrary `UIView` subclass (even a non-reusable, non-cell view) designed in a XIB by simply marking it as `NibLoadable`:

```swift
class NibBasedRandomView: UIView, NibLoadable {
  // The rest of the view code goes here
}
```

### Example with a UIView in XIB's File's Owner

`Reusable` can also be used to load a `UIView` in Xib's File's Owner (even a non-reusable, non-cell view) designed in a XIB by simply marking it as `NibOwnLoadable`:

```swift
class NibBasedRandomView: UIView, NibOwnLoadable {
  // The rest of the view code goes here
}
```

## Registering your cells by code

Then to use those cells, you'll register them like this, without the need to manipulate any `reuseIdentifier` anywhere in the code:

```swift
class MyViewController: UIViewController {
  @IBOutlet private weak var tableView: UITableView!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    tableView.registerReusableCell(CodeBasedCustomCell) // This will register using the class without using a UINib
    tableView.registerReusableCell(NibBasedCustomCell) // This will register using NibBasedCustomCell.xib
  }
}
```

> Note: If your cell is prototyped in a Storyboard, there is no need to call `registerReusableCell` for those cell prototypes, as the Storyboard auto-register these cell prototypes with the `UITableView` / `UICollectionView` they are prototyped into.

## Dequeuing your cells

Then in your implementation of `UIViewControllerDataSource`, especially to dequeue a cell you will be able to do:

```swift
{'fraction_non_alphanumeric': 0.0817592846062597, 'fraction_numerical': 0.002007482434528698, 'mean_word_length': 4.802011646373742, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '276766', 'n_tokens_mistral': 3339, 'n_tokens_neox': 3100, 'n_words': 1369}