One-liner replacement for registering, dequeuing, casting cells for UITableView and UICollectionView
Instead of:
tableView.register(MyCustomCell.self, forCellReuseIdentifier: "MyCustomCell")
let cell : MyCustomCell = tableView.dequeueReusableCell(withIdentifier: "MyCustomCell", for: indexPath) as! MyCustomCell
you can now do only:
let : MyCustomCell = tableView.dequeueReusableCell(forIndexPath: indexPath)
No code refacotring needed, you can use that with existing projects that uses old-way. No need to worry about any registering, reuse identifiers, casting any more.
Regardless the type of cell (loaded from code, or loaded from nib file), code is always the same:
Only requirement is to make a xib file with the same name as a class name.
let : MyCustomCell = tableView.dequeueReusableCell(forIndexPath: indexPath)
let : MyCustomCellLoadedFromXib = tableView.dequeueReusableCell(forIndexPath: indexPath)
or
tableView.dequeueReusableCell(forIndexPath: indexPath) as MyCustomCell
tableView.dequeueReusableCell(forIndexPath: indexPath) as MyCustomCellLoadedFromXib
This pod will search for a xib file that matches class name and register this nib automatically, otherwise it registers cell by class name.
To run the example project, clone the repo, and run pod install
from the Example directory first.
CellRegistration is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "CellRegistration"
Grzegorz Krukowski, [email protected]
CellRegistration is available under the MIT license. See the LICENSE file for more info.