Idiomatic instantiation and dequeuing

Overview

ReusableViews

CI Status Version License Platform

Why?

This extension package solves the following problems:

  • Forces your team to adhere to standard practices by:
    • Making storyboard identifiers the same as their class names
    • Making UITableViewCell and UICollectionViewCell reuse identifiers the same as their class name
    • Making UICollectionView supplementary views and UITableViewHeaderFooterView reuse identifiers the same as their class name
  • Removes the need to force unwrap or force cast cells/headers as you dequeue
  • Removes the need to force unwrap or force cast UIViewControllers as you instantiate them from a storyboard

If you're a user of SwiftLint, you'll greatly appreciate the reduced number of warnings and no more need for snippets in the vein of:

guard let cell = tableView.dequeueReusableCell(withIdentifier: "...", for: indexPath) as? MyCustomCellType else {
  fatalError("We didn't get the cell")
}

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Convenient Instantiation of a View From a Nib

Requires that your view conforms to NibLoadableView.

let view = MyNibLoadableView.create()

Instantiation of a UIViewController From a Storyboard

Requires that a class has the same storyboard identifier as its class name.

let myStoryboard: UIStoryboard = ...
let myCustomViewController = myStoryboard.instantiateViewControllerOfType(MyCustomViewController.self) as MyCustomViewController

Dequeueing and Registering a UITableViewCell

Requires that a class has the same reuse identifier as its class name. You must register your cell type first.

// Registration
tableView.register(MyCustomCellType.self)

// Dequeueing
let cell = tableView.dequeueReusableCell(for: indexPath) as MyCustomCellType

Dequeueing and Registering a UICollectionViewCell

Requires that a class has the same reuse identifier as its class name. You must register your cell type first.

// Registration
collectionView.register(MyCustomCellType.self)

// Dequeueing
let cell = collectionView.dequeueReusableCell(for: indexPath) as MyCustomCellType

Dequeueing and Registering a UITableViewHeaderFooterView

Requires that a class has the same reuse identifier as its class name. You must register your cell type first.

// Registration
tableView.register(MyCustomHeaderFooterView.self)

/// Dequeueing
let header = tableView.dequeueReusableHeaderFooterView(inSection: section) as MyCustomHeaderFooterView

Dequeueing and Registering a UICollectionView Supplementary View

Requires that a class has the same reuse identifier as its class name. You must register your view type first.

// Registration
collectionView.register(MyCustomSupplementaryView.self, forSupplementaryViewElementOfKind: .sectionHeader) // or .sectionFooter

// Dequeueing
let view = collectionView.dequeueReusableSupplementaryView(ofKind: .sectionHeader, for: indexPath) as MyCustomSupplementaryView // also takes .sectionFooter

Registering a view backed by a nib

Views backed by nibs must implement the NibLoadableView protocol. The protocol has a pre-defined extension, such that your view doesn't need to add any methods or properties.

class MyCoolCell: UITableViewCell, NibLoadableView {
  ...
}

Installation

ReusableViews is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "ReusableViews"

Author

Hesham Salman, [email protected]

Twitter: @WhatsASoftware

License

ReusableViews is available under the MIT license. See the LICENSE file for more info.

You might also like...
Async+ for Swift provides a simple chainable interface for your async and throwing code, similar to promises and futures
Async+ for Swift provides a simple chainable interface for your async and throwing code, similar to promises and futures

Async+ for Swift provides a simple chainable interface for your async and throwing code, similar to promises and futures. Have the best of both worlds

A lightweight extension to Swift's CollectionDifference, supporting moves in addition to removals and insertions, critical when updating interfaces and managing reference types.

DifferenceTracker is a lightweight extension to Swift's CollectionDifference. It defines moves in addition to removals and insertions, critical when updating interfaces and managing reference types.

ZIP Foundation is a library to create, read and modify ZIP archive files.
ZIP Foundation is a library to create, read and modify ZIP archive files.

ZIP Foundation is a library to create, read and modify ZIP archive files. It is written in Swift and based on Apple's libcompression for high performa

BFKit-Swift is a collection of useful classes, structs and extensions to develop Apps faster.
BFKit-Swift is a collection of useful classes, structs and extensions to develop Apps faster.

Features • Classes and Extensions Compatibility • Requirements • Communication • Contributing • Installing and Usage • Documentation • Changelog • Exa

Butterfly is a lightweight library for integrating bug-report and feedback features with shake-motion event.
Butterfly is a lightweight library for integrating bug-report and feedback features with shake-motion event.

Butterfly is a lightweight library for integrating bug-report and feedback features with shake-motion event. Goals of this project One of th

Swifty closures for UIKit and Foundation
Swifty closures for UIKit and Foundation

Closures is an iOS Framework that adds closure handlers to many of the popular UIKit and Foundation classes. Although this framework is a substitute f

💻 A fast and flexible O(n) difference algorithm framework for Swift collection.
💻 A fast and flexible O(n) difference algorithm framework for Swift collection.

A fast and flexible O(n) difference algorithm framework for Swift collection. The algorithm is optimized based on the Paul Heckel's algorithm. Made wi

Ethereum Wallet Toolkit for iOS - You can implement an Ethereum wallet without a server and blockchain knowledge.
Ethereum Wallet Toolkit for iOS - You can implement an Ethereum wallet without a server and blockchain knowledge.

Introduction EtherWalletKit is an Ethereum Wallet Toolkit for iOS. I hope cryptocurrency and decentralized token economy become more widely adapted. H

How Swift standard types and classes were supposed to work.
How Swift standard types and classes were supposed to work.

How Swift standard types and classes were supposed to work. A collection of useful extensions for the Swift Standard Library, Foundation, and UIKit.

Comments
  • Swift 4 Update

    Swift 4 Update

    Includes:

    • zero functional code changes in the library.
    • A few redundant protocol conformance checks removed
    • Updated dependencies (Quick, Nimble)
    • Added swift-version file
    opened by Iron-Ham 0
Releases(2.0.0)
Owner
Hesham Salman
it's always time for second lunch
Hesham Salman
Useful Swift code samples, extensions, functionalities and scripts to cherry-pick and use in your projects

SwiftyPick ?? ?? Useful Swift code samples, extensions, functionalities and scripts to cherry-pick and use in your projects. Purpose The idea behind t

Manu Herrera 19 May 12, 2022
Validate iOS, Android, and Mac localizations. Find errors in .strings, .stringsdict, and strings.xml files.

Locheck An Xcode and Android localization file validator. Make sure your .strings, .stringsdict, and strings.xml files do not have any errors! What do

Asana 73 Dec 13, 2022
Swift-DocC is a documentation compiler for Swift frameworks and packages aimed at making it easy to write and publish great developer documentation.

Swift-DocC is a documentation compiler for Swift frameworks and packages aimed at making it easy to write and publish great developer docum

Apple 833 Jan 3, 2023
Customize and resize sheets in SwiftUI with SheeKit. Utilise the power of `UISheetPresentationController` and other UIKit features.

SheeKit Customize and resize sheets in SwiftUI with SheeKit. Utilise the power of UISheetPresentationController and other UIKit features. Overview She

Eugene Dudnyk 67 Dec 31, 2022
A handy collection of Swift method and Tools to build project faster and more efficient.

SwifterKnife is a collection of Swift extension method and some tools that often use in develop project, with them you might build project faster and

李阳 4 Dec 29, 2022
Tools and helpers to make building apps faster and safer.

The UBFoundation framework provides a set of useful tools and helpers to make building apps faster and safer.

Ubique 7 Dec 19, 2022
Paul Hudson redesigned and remastered the original MoonshotApp. And so I followed the tutorial

Moonshot App for iOS. parsing some json files, using generics, and creating multi page app. Application provides simple informations from the Apollo m

Pavel Surový 0 Dec 7, 2021
Easy CBOR encoding and decoding for iOS, macOS, tvOS and watchOS.

CBORCoding CBORCoding is a lightweight framework containing a coder pair for encoding and decoding Codable conforming types to and from CBOR document

Joe Newton 23 Nov 8, 2022
TypeStyle is a handy app for iPhone and iPad that generates text using different styles and decorations. It is a native Swift iOS app.

TypeStyle TypeStyle is a handy app for iPhone and iPad that generates text using different styles and decorations. It is a native Swift iOS app. Featu

Eugene Belinski 31 Dec 14, 2022
An open source Instapaper clone that features apps and extensions that use native UI Components for Mac and iOS.

TODO: Screenshot outdated Hipstapaper - iOS and Mac Reading List App A macOS, iOS, and iPadOS app written 100% in SwiftUI. Hipstapaper is an app that

Jeffrey Bergier 51 Nov 15, 2022