UITableView: how to disable dragging of items to a specific row? UITableView: how to disable dragging of items to a specific row? ios ios

UITableView: how to disable dragging of items to a specific row?


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:

  • examples
  • to know what to do if the proposed move is not okay

Without further ado, here are some

Examples

Accept all moves

- (NSIndexPath *)tableView:(UITableView *)tableView    targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath                         toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{    return proposedDestinationIndexPath;}

Reject all moves, returning the row to its initial position

- (NSIndexPath *)tableView:(UITableView *)tableView    targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath                         toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{    return sourceIndexPath;}

Reject some moves, returning any rejected row to its initial position

- (NSIndexPath *)tableView:(UITableView *)tableView    targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath                         toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{    if (... some condition ...) {        return sourceIndexPath;    }    return proposedDestinationIndexPath;}


How to make last row fix:

- (NSIndexPath *)tableView:(UITableView *)tableViewtargetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath       toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{    // get number of objects    NSUInteger numberOfObjects = [[[BNRItemStore sharedStore] allItems] count];     if ( (proposedDestinationIndexPath.row+1==numberOfObjects) || (sourceIndexPath.row+1==numberOfObjects) ) {        NSLog(@"HERE");        return sourceIndexPath;    }    else{         NSLog(@"count=%d %d", [[[BNRItemStore sharedStore] allItems] count], proposedDestinationIndexPath.row);        return proposedDestinationIndexPath;    }}