I\'ve got some rather complicated rules for moving rows around in a UITableView
. There are an undefined number of sections and rows per section, and based on v
Here Cityarray is an array...
id obj =[Cityarray objectatindex:sourceindexpath.row];
[Cityarray removeObjectatindex:(sourceindexpath.row)];
[Cityarray insertObject:obj atindex:destinationindexpath.row];
I haven't visited this problem in quite awhile. The solution I came up with was removing the complicated rules. Not sure why they allowed usage of complicated rules if it crashed the application when using them. Not sure if this is fixed in the latest version of the OS or not.
I just happen to witness this problem. However, my version of crash seems to occur on an iOS 3.0 device that I'm trying to support, and I have only two rows in the table that I'm trying to rearrange. I ran the app again with the same code on another device with iOS 4.0 and that bug seems to have been fixed.
I'm still researching this, but as of now, I'm disabling moving of rows on devices with iOS 3.0 until a fix is found.
Are you updating your data model for the table in targetIndexPathForMoveFromRowAtIndexPath
Or in the DataSource delegate method: tableView:moveRowAtIndexPath:toIndexPath:
?
Taken from the Table View Programming Guide for iPhone OS, under reordering table cells:
The table view sends tableView:moveRowAtIndexPath:toIndexPath: to its data source (if it implements the method). In this method the data source updates the data-model array that is the source of items for the table view, moving the item to a different location in the array.
And for the delegate method, it is written:
Every time the dragged row is over a destination, the table view sends tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath: to its delegate (if it implements the method). In this method the delegate may reject the current destination for the dragged row and specify an alternative one.
tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:
is for determining whether a relocation is allowed, but actual changes to your data model should take place in tableView:moveRowAtIndexPath:toIndexPath:
Perhaps this is what you're doing, but I can't tell just from the info you provided.
Seems like something somewhere is requesting element 6 from an array that only has elements at indexes 0-5
(meaning 6 elements).
This usually happens when the code tries to do:
NSUInteger index = [somearray count];
id obj = [somearray objectAtIndex:index];
because count
is upper boundary and arrays start from 0 the last element is at count - 1
.
This might not be directly in your code but you may be restricting something to a number of elements and then requesting one past the last element.
I had a similar error with deleting that I couldn't figure out for a while -- but I put [tableView beginUpdates]
and [tableView endUpdates]
around the code and it fixed everything. Could be that your datasource just isn't updating before it attempts to redraw, and those methods should prevent that (worth a shot, anyway).