-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f17065c
commit 819beec
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import UIKit | ||
|
||
protocol Reusable: class { | ||
static var reuseIdentifier: String { get } | ||
static var nib: UINib? { get } | ||
} | ||
|
||
extension Reusable { | ||
static var reuseIdentifier: String { return String(Self) } | ||
static var nib: UINib? { return nil } | ||
} | ||
|
||
extension UITableView { | ||
func registerReusableCell<T: UITableViewCell where T: Reusable>(_: T.Type) { | ||
if let nib = T.nib { | ||
self.registerNib(nib, forCellReuseIdentifier: T.reuseIdentifier) | ||
} else { | ||
self.registerClass(T.self, forCellReuseIdentifier: T.reuseIdentifier) | ||
} | ||
} | ||
|
||
func dequeueReusableCell<T: UITableViewCell where T: Reusable>(indexPath indexPath: NSIndexPath) -> T { | ||
return self.dequeueReusableCellWithIdentifier(T.reuseIdentifier, forIndexPath: indexPath) as! T | ||
} | ||
|
||
func registerReusableHeaderFooterView<T: UITableViewHeaderFooterView where T: Reusable>(_: T.Type) { | ||
if let nib = T.nib { | ||
self.registerNib(nib, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier) | ||
} else { | ||
self.registerClass(T.self, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier) | ||
} | ||
} | ||
|
||
func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView where T: Reusable>() -> T? { | ||
return self.dequeueReusableHeaderFooterViewWithIdentifier(T.reuseIdentifier) as! T? | ||
} | ||
} | ||
|
||
extension UICollectionView { | ||
func registerReusableCell<T: UICollectionViewCell where T: Reusable>(_: T.Type) { | ||
if let nib = T.nib { | ||
self.registerNib(nib, forCellWithReuseIdentifier: T.reuseIdentifier) | ||
} else { | ||
self.registerClass(T.self, forCellWithReuseIdentifier: T.reuseIdentifier) | ||
} | ||
} | ||
|
||
func dequeueReusableCell<T: UICollectionViewCell where T: Reusable>(indexPath indexPath: NSIndexPath) -> T { | ||
return self.dequeueReusableCellWithReuseIdentifier(T.reuseIdentifier, forIndexPath: indexPath) as! T | ||
} | ||
|
||
func registerReusableSupplementaryView<T: Reusable>(elementKind: String, _: T.Type) { | ||
if let nib = T.nib { | ||
self.registerNib(nib, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: T.reuseIdentifier) | ||
} else { | ||
self.registerClass(T.self, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: T.reuseIdentifier) | ||
} | ||
} | ||
|
||
func dequeueReusableSupplementaryView<T: UICollectionViewCell where T: Reusable>(elementKind: String, indexPath: NSIndexPath) -> T { | ||
return self.dequeueReusableSupplementaryViewOfKind(elementKind, withReuseIdentifier: T.reuseIdentifier, forIndexPath: indexPath) as! T | ||
} | ||
} |