24721

Rearranging Cells in UITableView Bug & Saving Changes

When rearranging cells in my <strong>custom</strong> UITableView I am encountering a bug where for e.g.

If I am rearranging 1,2,3,4,5 and I move cell 1 to the end where cell 5 is then what happens is all the cells rename themselves to whichever cell they are replacing. So instead of the new order being 2,3,4,5,1 it becomes 1(2),2(3),3(4),4(5),1(moved cell).

In my core data model I have two entities one for <strong>Folder</strong> and another for <strong>Items</strong>. It is a to many relationship of type NSSet so Folder can have many Items. Items have a property called index of type Int and <strong>represents the items order in the array</strong> & the Folder has transient property called arrayOfItems which of type transformable. This returns an array of the Items ordered by their index value.

The console prints out end of didmovecell function <strong>but not</strong> Moved cell.

table.didMoveCellFromIndexPathToIndexPathBlock = {(fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) -> Void in func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { let itemThatMoved = self.selectedFolder.arrayOfItems[sourceIndexPath.row] self.selectedFolder.arrayOfItems.removeAtIndex(sourceIndexPath.row) self.selectedFolder.arrayOfItems.insert(itemThatMoved, atIndex:destinationIndexPath.row ) var currentIndex = 0 for item in self.selectedFolder.arrayOfItems { item.index=currentIndex currentIndex++ println("Moved cell") } } println("end of didmovecell function") }

Any ideas why this might be happening and how I can save the changes to my core data model e.g. updating the item Index value.

PS: I have tested didMoveCellFromIndexPathToIndexPathBlock for my custom UITableView <strong>without using core data</strong> with a basic array called Objects. It worked fine with something like this;

table.didMoveCellFromIndexPathToIndexPathBlock = {(fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) -> Void in self.objects.exchangeObjectAtIndex(toIndexPath.row, withObjectAtIndex: fromIndexPath.row) }

Answer1:

Ok, if you need to keep the blocks then change your code to:

table.didMoveCellFromIndexPathToIndexPathBlock = {(fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) -> Void in let itemThatMoved = self.selectedFolder.arrayOfItems[fromIndexPath.row] self.selectedFolder.arrayOfItems.removeAtIndex(fromIndexPath.row) self.selectedFolder.arrayOfItems.insert(itemThatMoved, atIndex:toIndexPath.row ) var currentIndex = 0 for item in self.selectedFolder.arrayOfItems { item.index=currentIndex currentIndex++ } // at this point call saveContext() to ensure that rearrangement is saved in Core Data }

Previously you had a function wrapped in your block. You were calling the block but the function within it was never called :)

Recommend

  • Core data: What should I type in the “Value Transformer Name”
  • How do I create a core data entry with a queryable array property?
  • android Locale.Builder() doesn't mirror the page in right-to-left locales
  • Check command for validity with data from other aggregate
  • Improve function that converts serialized object in dot notation into js object
  • How to remove minimize/maximize buttons while preserving the icon?
  • D: how to extract data from archive?
  • QAugmentedReality on BB10
  • How to add a user in a different Active Directory Domain in C#?
  • How can I pass arguments to ranlib using cmake?
  • Open source augmented reality framework for BlackBerry
  • Can I Modify Hibernate/JPA Attribute Dynamically?
  • For a np.array([1, 2, 3]) why is the shape (3,) instead of (3,1)? [duplicate]
  • Building libiconv fails with the Android standalone toolchain
  • how to make NSManagedObjectContext dirty (hasChanges = YES) Manually
  • SQL Server - Get all children of a row in many-to-many relationship?
  • How to prevent stack overflow when using Scala's Predef.implicitly
  • OpenMP for dependent variables
  • Rails 4 order by virtual attribute
  • Equivalent for np.add.at in tensorflow
  • R Leaflet Legend: specify order instead of alphabetical
  • Lua string.gsub() by '%s' or '\\n' pattern
  • How to estimate the Kalman Filter with 'KFAS' R package, with an AR(1) transition equation
  • Create Instant using a negative year
  • Meteor helpers not available in Angular template
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Regex thinks I'm nesting, but I'm not
  • Fill an image in a square container while keeping aspect ratio
  • MySQL WHERE-condition in procedure ignored
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper way to use connect-multiparty with express.js?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Sorting a 2D array using the second column C++
  • Reading document lines to the user (python)
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal