Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a UITableViewCell being moved away from its own section?

My UITableView has 3 sections. Only the cells in the 2nd sections are movable:

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL canMove = NO;
    if (indexPath.section == 1) {
        canMove = YES;
    }
    return canMove;
}

However, a cell (B) in the 2nd section (originally contains A``B``C cells) can be moved to the 1st section (originally contains only the T cell):

  enter image description here

How can I make sure cells are moved within its own section?

like image 655
ohho Avatar asked Jun 20 '12 11:06

ohho


1 Answers

I think this will solve your problem

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

From Apple documentation

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 a another row, the destination row slides downward to visually make room for the relocation; this is the location identified by proposedDestinationIndexPath.

like image 92
Malek_Jundi Avatar answered Nov 03 '22 14:11

Malek_Jundi