NickolaySheika / DTTableViewManager

The most simple and robust way to manage UITableView

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status   CocoaPod platform   CocoaPod version   License MIT

DTTableViewManager

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

Features

  • 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

Workflow

Here are 4 simple steps you need to use DTTableViewManager:

  1. Your view controller should subclass DTTableViewController, and set tableView property.
  2. You should have subclass of DTTableViewCell.
  3. In your viewDidLoad method, call mapping methods to establish relationship between data models and UITableViewCells.
  4. Add data models to memoryStorage, or use CoreData storage class.

API quickstart

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.

Mapping

  • 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.

Storyboards

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.

Managing table items

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.

Memory storage

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.

Search

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.

Core Data storage

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];
Search

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.

Requirements

  • iOS 6.0 and later
  • ARC

Installation

Simplest option is to use CocoaPods:

pod 'DTTableViewManager', '~> 2.7.0'

Documentation

You can view documentation online or you can install it locally using cocoadocs!

Thanks

  • Alexey Belkevich for providing initial implementation of CellFactory.
  • Michael Fey for providing insight into NSFetchedResultsController updates done right.

About

The most simple and robust way to manage UITableView

License:MIT License


Languages

Language:Objective-C 60.4%Language:Objective-C++ 37.3%Language:C 1.9%Language:Ruby 0.4%