Easy way to setup custom cells with xib
- iOS 8.0+
- Xcode 8.0+
- Swift 3.0+
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ 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
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
target '<Your Target Name>' do
pod 'RSReusable'
end
Then, run the following command:
$ pod install
Make sure you name your cell's class and xib with the same names
- MyCell.swift:
class MyCell: UITableViewCell, Reusable {
- MyCell.xib
- Make cell to implement Reusable protocol
import Reusable
//...
class MyCell: UITableViewCell, Reusable {
- (optional) Make cusom xib cell and set cell's class to MyCell
- Dequeue cell
import Reusable
//...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeue(forIndexPath: indexPath) as MyCell
//setup cell ...
return cell
}
- Make header (footer) to inherit from BaseTableSectionHeaderFooterView class
import Reusable
//...
class MySectionHeaderView: BaseTableSectionHeaderFooterView {
- (optional) Make cusom xib view and set file's owner to your class name - MySectionHeaderView
- Dequeue header (footer) view
import Reusable
//...
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueHeaderFooterView() as MySectionHeaderView
//setup header ...
return header
}
- Make cell to implement Reusable protocol
import Reusable
//...
class MyCollectionCell: UICollectionViewCell, Reusable {
- (optional) Make cusom xib cell and set cell's class to MyCollectionCell
- Dequeue cell
import Reusable
//...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueCell(for: indexPath) as MyCollectionCell
//setup cell ...
return cell
}
- Make header (footer) to implement Reusable protocol
import Reusable
//...
class CollectionHeaderView: UICollectionReusableView, Reusable {
- (optional) Make cusom xib view (UICollectionReusableView) and set it's class to CollectionHeaderView
- Dequeue view
import Reusable
//...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueCell(for: indexPath) as CollectionHeaderView
//setup header ...
return header
}
Roman Sorochak - iOS developer. You may contact me via email: [email protected]
Reusable is released under the MIT license. See LICENSE for details.