This is a sister-project for DTCollectionViewManager - great tool for UICollectionView management, built on the same principles.
Target of this project is to create powerful architecture for UITableView сontrollers. It combines several ideas to make UITableView management easy, clean, and delightful.
Try it out! =)
pod try DTTableViewManager
- Powerful mapping system between data models and table view cells, headers and footers
- Automatic datasource and interface synchronization.
- Support for creating cells from code, XIBs or storyboards.
- Easy UITableView search
- Core data / NSFetchedResultsController support
Here are 4 simple steps you need to use DTTableViewManager:
- Your view controller should subclass
DTTableViewController
, and set tableView property. - You should have subclass of
DTTableViewCell
. - In your viewDidLoad method, call mapping methods to establish relationship between data models and UITableViewCells.
- Add data models to memoryStorage, or use CoreData storage class.
Key classes | |
---|---|
DTTableViewController | Your UIViewController, that presents tableView, needs to subclass* this class. This class implements all UITableViewDatasource methods. |
DTMemoryStorage | Class responsible for holding tableView models. It is used as a default storage by DTTableViewManager. |
DTCoreDataStorage | Class used to display data, using `NSFetchedResultsController`. |
DTSectionModel | Object, representing section in UITableView. Basically has three properties - array of objects, headerModel and footerModel. |
Protocols | |
DTModelTransfer | Protocol, which methods are used to transfer model to UITableViewCell subclass, that will be representing it. |
Convenience classes (optional) | |
DTTableViewCell | UITableViewCell subclass, conforming to `DTModelTransfer` protocol. |
DTDefaultCellModel | Custom model class, that allows to use UITableViewCell without subclassing. |
DTDefaultHeaderFooterModel | Custom model class, that allows to use UITableViewHeaderFooterView without subclassing. |
- If you need your view controller to be subclassed from something else than DTTableViewController, it's good practice to use UIViewController containment API, and embed DTTableViewController subclass as a child inside inside parent controller.
- Cells
[self registerCellClass:[Cell class] forModelClass:[Model class]];
- Headers/Footers
[self registerHeaderClass:[HeaderView class] forModelClass:[Model class]];
[self registerFooterClass:[FooterView class] forModelClass:[Model class]];
This will also register nibs with Cell
, HeaderView
and FooterView
name, if any of them exist.
If you use storyboards and prototype cells, you will need to set reuseIdentifier for corresponding cell in storyboard. Reuse identifier needs to be identical to your cell class name.
Storage classes for DTTableViewManager have been moved to separate repo. Two data storage classes are provided - memory and core data storage. Let's start with DTMemoryStorage
, that is used by default.
DTMemoryStorage
encapsulates storage of table view data models in memory. It's basically NSArray of DTSectionModel
objects, which contain array of objects for current section, section header and footer model.
To work with memory storage, you will need to get it's instance from your DTTableViewController
subclass.
- (DTMemoryStorage *)memoryStorage;
You can take a look at all provided methods for manipulating items here: DTMemoryStorage methods
In most cases, adding items to memory storage is as simple as calling:
- (void)addItem:(NSObject *)item;
- (void)addItems:(NSArray *)items toSection:(NSInteger)sectionNumber;
DTTableViewManager adds several methods to DTMemoryStorage
, that are specific to UITableView. Two most relevant of them are
- (void)setSectionHeaderModels:(NSArray *)headerModels;
- (void)setSectionFooterModels:(NSArray *)footerModels;
These methods allow setting header and footer models for multiple sections in single method call.
Set UISearchBar's delegate property to your DTTableViewController
subclass.
Call memoryStorage setSearchingBlock:forModelClass: to determine, whether model of passed class should show for current search criteria. This method can be called as many times as you need.
[self.memoryStorage setSearchingBlock:^BOOL(id model, NSString *searchString, NSInteger searchScope, DTSectionModel *section)
{
Example * example = model;
if ([example.text rangeOfString:searchString].location == NSNotFound)
{
return NO;
}
return YES;
} forModelClass:[Example class]];
Searching data storage will be created automatically for current search, and it will be used as a datasource for UITableView.
DTCoreDataStorage
is meant to be used with NSFetchedResultsController. It automatically monitors all NSFetchedResultsControllerDelegate methods and updates UI accordingly to it's changes. All you need to do to display CoreData models in your UITableView, is create DTCoreDataStorage object and set it on your DTTableViewController subclass.
self.dataStorage = [DTCoreDataStorage storageWithFetchResultsController:controller];
Subclass DTCoreDataStorage and implement single method
- (instancetype)searchingStorageForSearchString:(NSString *)searchString
inSearchScope:(NSInteger)searchScope;
You will need to provide a storage with NSFetchedResultsController and appropriate NSPredicate. Take a look at example application, that does just that.
- iOS 6.0 and later
- ARC
Simplest option is to use CocoaPods:
pod 'DTTableViewManager', '~> 2.7.0'
You can view documentation online or you can install it locally using cocoadocs!
- Alexey Belkevich for providing initial implementation of CellFactory.
- Michael Fey for providing insight into NSFetchedResultsController updates done right.