Saturday, June 8, 2013

UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath example in Objective C (iOS).


UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath

Asks the delegate to return a new index path to retarget a proposed move of a row.

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

Parametersof [UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath]
tableView
The table-view object that is requesting this information.
sourceIndexPath
An index-path object identifying the original location of a row (in its section) that is being dragged.
proposedDestinationIndexPath
An index-path object identifying the currently proposed destination of the row being dragged.

Return Value of [UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath]
An index-path object locating the desired row destination for the move operation. Return proposedDestinationIndexPath if that location is suitable.

Discussion of [UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath]
This method allows customization of the target row for a particular row as it is being moved up and down a table view. As the dragged row hovers over another row, the destination row slides downward to visually make room for the relocation; this is the location identified by proposedDestinationIndexPath.

UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath example.
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
  if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
    NSInteger row = 0;
    if (sourceIndexPath.section < proposedDestinationIndexPath.section) {
      row = [tableView numberOfRowsInSection:sourceIndexPath.section] - 1;
    }
    return [NSIndexPath indexPathForRow:row inSection:sourceIndexPath.section];    
  }

  return proposedDestinationIndexPath;
}

Example of [UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath].
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if( sourceIndexPath.section != proposedDestinationIndexPath.section )
    {
        return sourceIndexPath;
    }
    else
    {
        return proposedDestinationIndexPath;
    }
}

UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath example.
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:sourceIndexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"Moving Cell to %d %d", proposedDestinationIndexPath.section, proposedDestinationIndexPath.row];
    return proposedDestinationIndexPath;
}

End of UITableView tableView targetIndexPathForMoveFromRowAtIndexPath toProposedIndexPath example article.