Skip to content

Instantly share code, notes, and snippets.

@anthonyvitocuaderno
Created June 5, 2018 09:02
Show Gist options
  • Save anthonyvitocuaderno/e194e1975683b5cf5a34f94bb5080961 to your computer and use it in GitHub Desktop.
Save anthonyvitocuaderno/e194e1975683b5cf5a34f94bb5080961 to your computer and use it in GitHub Desktop.
iOS Swift 3.3 UITableView extension
//
// UITableView.swift
// R2S
//
// Created by Vito Cuaderno on 4/30/18.
// Copyright © 2018 Total Integrated Resources. All rights reserved.
//
import UIKit
extension UITableView {
func registerCell<T:UITableViewCell>(_ cell:T.Type) {
let xib = String(describing: T.self)
self.register(UINib(nibName: xib, bundle: nil), forCellReuseIdentifier: xib)
}
func reloadDataWithFadeAnimation(){
UIView.transition(with: self,
duration: 0.35,
options: .transitionCrossDissolve,
animations: { self.reloadData() })
}
func dequeueCell<T:UITableViewCell>(_ cell:T.Type, for indexPath:IndexPath) -> T {
return self.dequeueReusableCell(withIdentifier: String(describing: T.self), for: indexPath) as! T
}
}
extension UICollectionView {
func registerHeader<T:UICollectionReusableView>(_ cell:T.Type) {
let xib = String(describing: T.self)
self.register(UINib(nibName: xib, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: xib)
}
func registerCell<T:UICollectionViewCell>(_ cell:T.Type) {
let xib = String(describing: T.self)
self.register(UINib(nibName: xib, bundle: nil), forCellWithReuseIdentifier: xib)
}
func registerFooter<T:UICollectionReusableView>(_ cell:T.Type) {
let xib = String(describing: T.self)
self.register(UINib(nibName: xib, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: xib)
}
func dequeueCell<T:UICollectionViewCell>(_ cell:T.Type, for indexPath:IndexPath) -> T {
return self.dequeueReusableCell(withReuseIdentifier: String(describing: T.self), for: indexPath) as! T
}
func dequeueHeader<T:UICollectionReusableView>(_ header:T.Type, for indexPath:IndexPath) -> T {
return self.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: String(describing: T.self), for: indexPath) as! T
}
func dequeueFooter<T:UICollectionReusableView>(_ footer:T.Type, for indexPath:IndexPath) -> T {
return self.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: String(describing: T.self), for: indexPath) as! T
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment