ASCollectionViewController icon indicating copy to clipboard operation
ASCollectionViewController copied to clipboard

A UICollectionViewController subclass that makes working with NSFetchedResultsController, UIMenuController and UIRefreshControl easier.

trafficstars

ASCollectionViewController

Badge w/ Version Badge w/ Platform

A UICollectionViewController subclass that makes working with NSFetchedResultsController, UIMenuController and UIRefreshControl easier.

In particular it:

  • Takes care of batching together any changes to your model to update the collectionView. This includes call backs if you need additional logic to respond to deletions/insertions/updates/moves.
  • Enables long-tap menus on cells using UIMenuItem objects.
  • Makes it easy to add a UIRefreshControl control.

The fetched results controller logic is taken from this gist which is based on Blake Watters' idea.

Setup

  1. Add to your Podfile (or manually add ASCollectionViewController.h and ASCollectionViewController.m):

     pod 'ASCollectionViewController', '~> 0.1.0'
    
  2. Subclass ASCollectionViewController instead of UICollectionViewController

Using the NSFetchedResultsController helper

  • Set the managedObjectContext property.
  • Then call one of the configureFetcher... methods.
  • Implement the numberOfSectionsInCollectionView:, collectionView:numberOfItemsInSection: and collectionView:cellForItemAtIndexPath: methods as usual. Note that you have access to the fetchedResultsController property to look up section info and objects.
  • Any changes to the CoreData model will automatically be reflected.
  • Implement the handleFetchResultsControllerError method as the default implementation is to crash with an abort() call.
  • Optionally, implement any of the didInsert/Delete/Update/MoveObject methods if you need to so something special.

Using the UIMenuController helper

  • Call addLongTapMenuItems: with the UIMenuItem objects that you'd like to enable.
  • Implement your selectors of those UIMenuItem objects. To get the element use the selection of the collection view.
  • Optionally overwrite the shouldShowMenuItemForAction:forItemAtIndexPath: method to customise which action/menu item should be enabled for which item in your collection view.

Using the UIRefreshControl helper

  • Call addRefreshControlWithTarget:action: to add a UIRefreshControl to your collection view.
  • Optionally, call removeRefreshControl to remove it again.

Example

See the included example project for a very basic implementation.