代码之家  ›  专栏  ›  技术社区  ›  Maggie

iPhone UITableViewController只移动表中的特定行

  •  4
  • Maggie  · 技术社区  · 15 年前

    我已经实施了一个标准 UITableViewController . 我在桌子上只做了几行可以移动的。 canMoveRowAtIndexPath 仅对某些indexpath(对于表的第一部分中的行)返回true。我只允许交换可移动的行 可移动浏览索引路径 返回true。

    我的桌子看起来像这样:

    Row 1
    Row 2
    Row 3
    Row 4
    Row 5
    

    第1、2、3行可移动。所以我想实现以下行为:行1只能与行2或3交换。同样,第2行只能与第1行和第3行交换。 这是一种可能的布局:

    Row 3
    Row 1
    Row 2
    Row 4
    Row 5
    

    但是,我不想这样:

    Row 3
    Row 5
    Row 2
    Row 4
    Row 1
    

    如何做到这一点?

    2 回复  |  直到 15 年前
        1
  •  4
  •   Community Mohan Dere    9 年前

    记住它实际上只移动了一排,而不是 交换 .

    你想要的是实现 tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath

    How to limit UITableView row reordering to a section

        2
  •  0
  •   Andy    11 年前

    您可以控制节内和节间的移动。

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

    // Do not allow any movement between section
    if ( sourceIndexPath.section != proposedDestinationIndexPath.section)
        return sourceIndexPath;
    // You can even control the movement of specific row within a section. e.g last row in a     Section
    
    // Check if we have selected the last row in section
    if (   sourceIndexPath.row < sourceIndexPath.length) {
        return proposedDestinationIndexPath;
    } else {
        return sourceIndexPath;
    }
    

    //可以使用此方法或逻辑检查节中行的索引,并返回sourceIndexPath或targetIndexPath

    }

    推荐文章