Skip to content

Instantly share code, notes, and snippets.

@KalpeshTalkar
Created August 25, 2020 13:29
Show Gist options
  • Save KalpeshTalkar/712281aa77120a265cd91d868841a39a to your computer and use it in GitHub Desktop.
Save KalpeshTalkar/712281aa77120a265cd91d868841a39a to your computer and use it in GitHub Desktop.
//
// Copyright © 2017 Kalpesh Talkar. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// For support: https://gist.github.com/KalpeshTalkar/712281aa77120a265cd91d868841a39a
//
import UIKit
// MARK: - Reuse Identifier Implementation (Requires `ReuseIdentifying.swift` https://gist.github.com/KalpeshTalkar/de566976a54823884f2329d399effe33)
/// UICollectionViewCell extension conforming to ReuseIdentifying
extension UICollectionViewCell: ReuseIdentifying {}
/// UITableViewCell extension conforming to ReuseIdentifying
extension UITableViewCell: ReuseIdentifying {}
// MARK: - UITableViewCell extension
extension UITableView {
// MARK: - Reusable Cell
/// Register cell from nib. Make sure the nib name is same as the class name.
/// - Parameter cell: Custom UITableVIewCell class.
public func register<T: UITableViewCell>(cell: T.Type) {
register(UINib(nibName: cell.reuseIdentifier, bundle: nil), forCellReuseIdentifier: T.reuseIdentifier)
}
/// Returns a reusable cell object located by its identifier.
///
/// - Parameters:
/// - indexPath: The index path specifying the location of the cell.
/// - type: The class of your custom table view cell.
/// - Returns: A valid UITableViewCell object.
public func dequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath, ofType type: T.Type) -> T? {
return dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T
}
/// Returns a reusable cell object located by its identifier.
///
/// - Parameters:
/// - type: The class of your custom table view cell.
/// - Returns: A valid UITableViewCell object.
public func dequeueReusableCell<T: UITableViewCell>(ofType type: T.Type) -> T? {
return dequeueReusableCell(withIdentifier: T.reuseIdentifier) as? T
}
// MARK: - Empty States
/// Check if the table view has data.
///
/// - Returns: true if has data else false.
func hasData() -> Bool {
let sections = numberOfSections
for section in 0..<sections {
let itemsInSec = numberOfRows(inSection: section)
if itemsInSec > 0 {
return true
}
}
return false
}
}
// MARK: - UITableViewCell extension
extension UICollectionView {
// MARK: - Reusable Cell
/// Register cell from nib. The method uses cellReuseIdentifier to register the cell from nib.
/// Make sure the nib name and class name are same.
/// - Parameter cell: The class of your UICollectionViewCell.
public func register<T: UICollectionViewCell>(cell: T.Type) {
register(UINib.init(nibName: T.reuseIdentifier, bundle: nil), forCellWithReuseIdentifier: T.reuseIdentifier)
}
/// Returns a reusable cell object located by its identifier.
///
/// - Parameters:
/// - indexPath: The index path specifying the location of the cell.
/// - type: The class of your custom collection view cell.
/// - Returns: A valid UICollectionReusableView object.
public func dequeueReusableCell<T: UICollectionViewCell>(for indexPath: IndexPath, ofType type: T.Type) -> T? {
return dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as? T
}
// MARK: - Empty States
/// Check if the collection view has data.
///
/// - Returns: true if has data else false.
func hasData() -> Bool {
let sections = numberOfSections
for section in 0..<sections {
let itemsInSec = numberOfItems(inSection: section)
if itemsInSec > 0 {
return true
}
}
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment