How to reorder a UITableView with Core Data

You’d imagine something as sophisticated as an NSFetchedResultsController has a built-in mechanism to let the user re-order cells arbitrarily. But that’s not the case, so we need to be inventive.

A table view can be re-ordered without problems, but how do we make the new order persist in Core Data? Looks like our managed objects need an integer property for that. In this example I’ll use a 16 bit integer called position – but you can call it anything you like.

There are several (more or less) complicated approaches to the rest of this puzzle. I will use Matt Long’s excellent and least complicated option by adding all fetched objects to an array. Let’s do this step by step, starting with the Xcode Master/Detail template. My managed object is called Phrase.

First we need to make sure our table view is reorder-able. Let’s do this by implementing

This content is for members only.

This is called automatically if the fetched results controller has noticed a change in its data structure. It grabs its new objects and refreshes the table view – and we’re done.

Here’s a full working project that implements this feature among other things:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.