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
UIMenuItemobjects. - Makes it easy to add a
UIRefreshControlcontrol.
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.handASCollectionViewController.mto your project. ** SubclassASCollectionViewControllerinstead ofUICollectionViewController
Using the NSFetchedResultsController helper
- Set the
managedObjectContextproperty. - Then call one of the
configureFetcher...methods. - Implement the
numberOfSectionsInCollectionView:,collectionView:numberOfItemsInSection:andcollectionView:cellForItemAtIndexPath:methods as usual. Note that you have access to thefetchedResultsControllerproperty to look up section info and objects. - Any changes to the CoreData model will automatically be reflected.
- Implement the
handleFetchResultsControllerErrormethod as the default implementation is to crash with anabort()call. - Optionally, implement any of the
didInsert/Delete/Update/MoveObjectmethods if you need to so something special.
Using the UIMenuController helper
- Call
addLongTapMenuItems:with theUIMenuItemobjects that you'd like to enable. - Implement your selectors of those
UIMenuItemobjects. 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.
Example
See the included example project for a very basic implementation.