Last active
March 20, 2023 19:04
-
-
Save dilipiOSDeveloper/ddd678fdd2f20eb4e209441d28aaccf2 to your computer and use it in GitHub Desktop.
Extension for CollectionView and TableView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public extension UICollectionView { | |
/** | |
Register nibs faster by passing the type - if for some reason the `identifier` is different then it can be passed | |
- Parameter type: UICollectionView.Type | |
- Parameter identifier: String? | |
*/ | |
func registerCell(type: UICollectionViewCell.Type, identifier: String? = nil) { | |
let cellId = String(describing: type) | |
register(UINib(nibName: cellId, bundle: nil), forCellWithReuseIdentifier: identifier ?? cellId) | |
} | |
/** | |
DequeueCell by passing the type of UICollectionViewCell and IndexPath | |
- Parameter type: UICollectionViewCell.Type | |
- Parameter indexPath: IndexPath | |
*/ | |
func dequeueCell<T: UICollectionViewCell>(withType type: UICollectionViewCell.Type, for indexPath: IndexPath) -> T? { | |
return dequeueReusableCell(withReuseIdentifier: type.identifier, for: indexPath) as? T | |
} | |
func scrollToIndex(index:Int,section:Int){ | |
let indexPath = IndexPath(row: index, section: section) | |
self.selectItem(at: indexPath, animated: true, scrollPosition: .centeredHorizontally) | |
} | |
} | |
public extension UICollectionReusableView { | |
static var identifier: String { | |
return String(describing: self) | |
} | |
} | |
public extension UITableView { | |
/** | |
Register nibs faster by passing the type - if for some reason the `identifier` is different then it can be passed | |
- Parameter type: UITableViewCell.Type | |
- Parameter identifier: String? | |
*/ | |
func registerCell(type: UITableViewCell.Type, identifier: String? = nil) { | |
let cellId = String(describing: type) | |
register(UINib(nibName: cellId, bundle: nil), forCellReuseIdentifier: identifier ?? cellId) | |
} | |
/** | |
DequeueCell by passing the type of UITableViewCell | |
- Parameter type: UITableViewCell.Type | |
*/ | |
func dequeueCell<T: UITableViewCell>(withType type: UITableViewCell.Type) -> T? { | |
return dequeueReusableCell(withIdentifier: type.identifier) as? T | |
} | |
/** | |
DequeueCell by passing the type of UITableViewCell and IndexPath | |
- Parameter type: UITableViewCell.Type | |
- Parameter indexPath: IndexPath | |
*/ | |
func dequeueCell<T: UITableViewCell>(withType type: UITableViewCell.Type, for indexPath: IndexPath) -> T? { | |
return dequeueReusableCell(withIdentifier: type.identifier, for: indexPath) as? T | |
} | |
/** | |
Reload UITableViewCell without scroll | |
- Parameter | |
*/ | |
func reloadDataWithoutScroll() { | |
self.reloadData() | |
self.layoutIfNeeded() | |
self.setContentOffset(self.contentOffset, animated: false) | |
} | |
/** | |
This method returns the indexPath of the cell that contains the specified view | |
- Parameter view: The view to find. | |
- Returns: The indexPath of the cell containing the view, or nil if it can't be found | |
*/ | |
//- USE: self.tableView.indexPathForView(button) | |
func indexPathForView(_ view: UIView) -> IndexPath? { | |
let center = view.center | |
let viewCenter = self.convert(center, from: view.superview) | |
let indexPath = self.indexPathForRow(at: viewCenter) | |
return indexPath | |
} | |
} | |
public extension UITableViewCell { | |
static var identifier: String { | |
return String(describing: self) | |
} | |
} | |
Usage: - | |
1. | |
guard let cell = tableView.dequeueCell(withType: LandingCell.self, for: indexPath) as? LandingCell else { | |
fatalError("LandingCell is not initialize...") | |
} | |
return cell | |
2. | |
guard let cell = tableView.dequeueCell(withType: VersionCell.self, for: indexPath) as? VersionCell else { | |
fatalError("VersionCell is not initialize...") | |
} | |
3. | |
tbl_Status.registerCell(type: VersionCell.self) | |
cv_Saved.registerCell(type: SavedVideoCell.self) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment