0

I am trying to follow this Ray Wenderlich tutorial that lets you rearrange tablecells where the datasource is an NSArray:

It uses the exchangeObjectAtIndexPath.row withObjectAtIndex:sourceIndexPath.row method to rearrange the data behind the tableview. Since the data is held in a mutable array, it applies this method to self.dataItems, the NSMutableArray holding the data as follows:

  NSMutableArray * data = [@[@"Get Milk!", @"Go to gym", @"Breakfast with Rita!", @"Call Bob", @"Pick up newspaper", @"Send an email to Joe", @"Read this tutorial!", @"Pick up flowers"] mutableCopy];

self.dataItems = data;
    // ... update data source.
        [self.dataItems exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];

In my case, I use a FetchedResultsController to pull the data from coredata and the datasource for the tableView is self.

self.tableView.dataSource = self;

Does anyone know if there is something analagous to an NSArray behind the FRC so that I can implement the same method or otherwise swap the dataItems around while still using an FRC?

Thanks for any suggestions.

user6631314
  • 1,751
  • 1
  • 13
  • 44
  • 1
    You can get a fetched results controller to sort the results it retrieves, it it needs some attribute to sort on, such as an integer "order" field. You would need to include such a field in your Core Data entity and then in your move function you would need to update the records and refer him the data. To be honest a FRC is probably going to work against you here. It may be just fetch the objects into an array. You would still need the sort field and to update this field as your moved things around, but at least you wouldn't need to keep re-fetching – Paulw11 Jul 05 '17 at 17:51
  • Do I have to conver the fetchedresults in the FRC into an array and use the exchangeObjectAtIndexPath method or is there an analagous method that would work directly on the contents of the FRC (to be honest I guess I don't know exactly what form the FRC holds its results in .) – user6631314 Jul 07 '17 at 10:12
  • Get rid of the FRC and just fetch the results into an array – Paulw11 Jul 07 '17 at 10:14
  • The problem is I'm using other stuff in the FRC in conjunction with the tableview. Are the FRC results an array themselves as suggested by this answer https://stackoverflow.com/questions/29449423/how-do-i-change-nsfetchresultcontroller-array-that-holds-my-objects-to-different – user6631314 Jul 07 '17 at 10:17
  • Essentially they are. A FRC makes it easier to respond to new objects inserted into the result set and results deleted from the result set, but if you don't need that then you can just query into an array – Paulw11 Jul 07 '17 at 10:18

0 Answers0