Iphone 在UITableView中移动行

Iphone 在UITableView中移动行,iphone,objective-c,Iphone,Objective C,我有一个包含3个部分的UITableView,在编辑模式下,可以在他的部分内更改行位置,而不更改为其他部分?实际上,我可以将单元格移动到任何部分 谢谢如果我正确理解了你的问题,那么是:使用 - (NSIndexPath*) tableView: (UITableView*) tableView targetIndexPathForMoveFromRowAtIndexPath: (NSIndexPath*) sourceIndexPath toProposedIndexPath: (NSIndex

我有一个包含3个部分的UITableView,在编辑模式下,可以在他的部分内更改行位置,而不更改为其他部分?实际上,我可以将单元格移动到任何部分


谢谢

如果我正确理解了你的问题,那么是:使用

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

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
if( fromIndexPath == toIndexPath ) {
return;
}

正如Steven和Narayanan所说,您必须实现该方法

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
您的UITableView委托的

在该方法中,您可以检查sourceIndexPath.com部分:

  • 如果是允许重新排序的部分,则返回proposedDestinationIndexPath
  • 如果不是,则返回sourceIndexPath,以便tableView不会移动该行
我没有编写代码,所以您可以很高兴地进行练习,而不是复制粘贴;)

(编辑:为选项添加列表样式)

添加此支票:

 if ( fromIndexPath.section != toIndexPath.section ) return; // prevent moving to another section.

许多答案都是正确的,但没有人给出一个完整和恰当的回答

使用表视图委托方法如下所示的表视图:targetIndexPathFormovfromRowAtIndexPath:TopProposedIndexPath:

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

    return proposedDestinationIndexPath;
}
不要忘记在表视图:moveRowatinexPath:toIndexPath:数据源方法中执行另一项检查,如下所示,当目标与源相似时,不希望运行应用程序逻辑

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath  *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    if(sourceIndexPath == destinationIndexPath)
    {
        return;        
    }

    //application logic
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath  *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    if(sourceIndexPath == destinationIndexPath)
    {
        return;        
    }

    //application logic
}