I have a UITableView with draggable rows and I can add/remove items. The datasource is a NSMutableArray.
Now, if I move the row with "Add new functionality" the app crashes because the dataSource is smaller since such row has not been added yet.
So I've modified this code:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row >= [dataList count]) return NO; return YES; }
And now I can't move it anymore. However I can still move the other rows after such row and consequently the code crashes.
How can I solve this ? Is there a way to disable the dragging "to" specific rows and not only from ?
thanks
This is exactly what the UITableViewDelegate
method
-tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:
is for. Will it suit your purposes? Here's the documentation.
The previous answers and the documentation (see this and this, as mentioned in the other answers) are helpful but incomplete. I needed:
Without further ado, here are some
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { return proposedDestinationIndexPath; }
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { return sourceIndexPath; }
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { if (... some condition ...) { return sourceIndexPath; } return proposedDestinationIndexPath; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With