https://github.com/romansorochak/reusable
Easy way to create/reuse custom cells & headers with xib written in Swift
https://github.com/romansorochak/reusable
cell collectionview custom dequeue ios reusable reuse swift tableview xib
Last synced: about 1 month ago
JSON representation
Easy way to create/reuse custom cells & headers with xib written in Swift
- Host: GitHub
- URL: https://github.com/romansorochak/reusable
- Owner: romansorochak
- License: mit
- Created: 2017-06-21T14:41:04.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-01-31T18:58:37.000Z (over 5 years ago)
- Last Synced: 2025-04-19T13:17:32.849Z (about 1 month ago)
- Topics: cell, collectionview, custom, dequeue, ios, reusable, reuse, swift, tableview, xib
- Language: Swift
- Homepage:
- Size: 22.5 KB
- Stars: 27
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Reusable
Easy way to setup custom cells with xib## Contents
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
- [Author](#author)
- [License](#license)## Requirements
- iOS 8.0+
- Xcode 8.0+
- Swift 3.0+## Installation
### CocoaPods
[CocoaPods](http://cocoapods.org) is a dependency manager for Cocoa projects. You can install it with the following command:
```bash
$ gem install cocoapods
```> CocoaPods 1.1.0+ is required to build Reusable 1.0.0+.
To integrate Reusable into your Xcode project using CocoaPods, specify it in your `Podfile`:
```ruby
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!target '' do
pod 'RSReusable'
end
```Then, run the following command:
```bash
$ pod install
```## Usage
Make sure you name your cell's class and xib with the same names
- MyCell.swift:
```swift
class MyCell: UITableViewCell, Reusable {
```- MyCell.xib
### UITableViewCell
#### Custom table cell with xib
1) Make cell to implement Reusable protocol
```swift
import Reusable
//...
class MyCell: UITableViewCell, Reusable {
```
2) (optional) Make cusom xib cell and set cell's class to MyCell
3) Dequeue cell
```swift
import Reusable
//...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeue(forIndexPath: indexPath) as MyCell
//setup cell ...
return cell
}
```
#### Custom table header (footer) with xib
1) Make header (footer) to inherit from BaseTableSectionHeaderFooterView class
```swift
import Reusable
//...
class MySectionHeaderView: BaseTableSectionHeaderFooterView {
```
2) (optional) Make cusom xib view and set file's owner to your class name - MySectionHeaderView
3) Dequeue header (footer) view
```swift
import Reusable
//...
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueHeaderFooterView() as MySectionHeaderView//setup header ...
return header
}
```### UICollectionViewCell
#### Custom collection cell with xib
1) Make cell to implement Reusable protocol
```swift
import Reusable
//...
class MyCollectionCell: UICollectionViewCell, Reusable {
```
2) (optional) Make cusom xib cell and set cell's class to MyCollectionCell
3) Dequeue cell
```swift
import Reusable
//...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueCell(for: indexPath) as MyCollectionCell
//setup cell ...
return cell
}
```
#### Custom collection header with xib
1) Make header (footer) to implement Reusable protocol
```swift
import Reusable
//...
class CollectionHeaderView: UICollectionReusableView, Reusable {
```
2) (optional) Make cusom xib view (UICollectionReusableView) and set it's class to CollectionHeaderView
3) Dequeue view
```swift
import Reusable
//...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueCell(for: indexPath) as CollectionHeaderView//setup header ...
return header
}
```## Author
Roman Sorochak - iOS developer. You may contact me via email: [email protected]## License
Reusable is released under the MIT license. See [LICENSE](https://github.com/romansorochak/Reusable/blob/master/LICENSE) for details.