ASCollectionViewController
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 Ash Furrow's code who in turn took it from this gist.
Setup
- Clone the repository
** Add
ASCollectionViewController.h
andASCollectionViewController.m
to your project. ** SubclassASCollectionViewController
instead ofUICollectionViewController
NSFetchedResultsController
helper
Using the - Set the
managedObjectContext
property. - Then call one of the
configureFetcher...
methods. - Implement the
numberOfSectionsInCollectionView:
,collectionView:numberOfItemsInSection:
andcollectionView:cellForItemAtIndexPath:
methods as usual. Note that you have access to thefetchedResultsController
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 anabort()
call. - Optionally, implement any of the
didInsert/Delete/Update/MoveObject
methods if you need to so something special.
UIMenuController
helper
Using the - Call
addLongTapMenuItems:
with theUIMenuItem
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.
UIRefreshControl
helper
Using the - Call
addRefreshControlWithTarget:action:
to add a UIRefreshControl to your collection view.
Example
See the included example project for a very basic implementation.