如何防止UITableViewCell从其自己的部分移开?

ohh*_*hho 3 iphone objective-c uitableview ios

UITableView有3个部分.只有第二部分中的单元格可以移动:

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

但是,B第二部分中的cell()(最初包含A``B``C单元格)可以移动到第一部分(最初只包含T单元格):

在此输入图像描述

如何确保单元格其自己的部分中移动?

Mal*_*ndi 9

我想这会解决你的问题

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

来自Apple 文档

此方法允许自定义特定行的目标行,因为它在表视图中上下移动.当拖动的行悬停在另一行上时,目标行向下滑动以在视觉上为重定位腾出空间; 这是proposedDestinationIndexPath标识的位置.