小智 8
许多正确的答案,但没有人提出完整和良好的答复.
使用表视图委托方法tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:如下所示.
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (proposedDestinationIndexPath.section != sourceIndexPath.section)
{
return sourceIndexPath;
}
return proposedDestinationIndexPath;
}
Run Code Online (Sandbox Code Playgroud)
不要忘记在tableView中执行另一次检查:moveRowAtIndexPath:toIndexPath:数据源方法如下所示,当目标与源类似时,您不希望运行应用程序逻辑.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
if(sourceIndexPath == destinationIndexPath)
{
return;
}
//application logic
}
Run Code Online (Sandbox Code Playgroud)