在 UITableView 中移动行

发布于 2024-11-08 16:57:36 字数 89 浏览 0 评论 0原文

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

谢谢

I have a UITableView that contain 3 sections, It is possible in the edit mode to change the row position just inside his section and not to another section?Actually I can move the cell to any section.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

懷念過去 2024-11-15 16:57:36

许多正确答案,但没有人给出完整且精美的答复。

使用表视图委托方法 tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath: ,如下所示。

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

    return proposedDestinationIndexPath;
}

不要忘记在您的 tableView:moveRowAtIndexPath:toIndexPath: 数据源方法中进行另一次检查,如下所示,您不希望在目标相似时运行应用程序逻辑到源头。

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

    //application logic
}

Many correct answers but no one presented a complete and well presented reply.

Use the table view delegate method tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath: as shown below.

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

    return proposedDestinationIndexPath;
}

Do not forget to do another check in your tableView:moveRowAtIndexPath:toIndexPath: data source method as shown below, you do not want to run your application logic when the destination is similar to the source.

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

    //application logic
}
披肩女神 2024-11-15 16:57:36

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

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

If I understand your question correctly, then yes: use

- (NSIndexPath*) tableView: (UITableView*) tableView targetIndexPathForMoveFromRowAtIndexPath: (NSIndexPath*) sourceIndexPath toProposedIndexPath: (NSIndexPath*) proposedDestinationIndexPath
翻了热茶 2024-11-15 16:57:36

就像 Steven 和 Narayanan 所说,你必须实现

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

UITableView 委托的方法。

在此方法中,您检查 sourceIndexPath.section:

  • 如果它是您允许重新排序的部分,则返回 suggestDestinationIndexPath。
  • 如果没有,您将返回 sourceIndexPath,这样您的 tableView 就不会移动该行。

我没有编写代码,因此您将有机会练习而不是复制粘贴;)

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

like Steven and Narayanan said, you have to implement the method

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

of your UITableView delegate.

In this method, you check the sourceIndexPath.section:

  • If it's the section you allow to be reordered, you return the proposedDestinationIndexPath.
  • If not, you return the sourceIndexPath so your tableView will not move the row.

I didn't write the code so you'll have the pleasure to practice instead of copy'n'paste ;)

(edit: adding list style for the options)

反目相谮 2024-11-15 16:57:36

添加

 moveRowAtIndexPath:toIndexPath:

此检查:

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

In

 moveRowAtIndexPath:toIndexPath:

add this check:

 if ( fromIndexPath.section != toIndexPath.section ) return; // prevent moving to another section.
谈下烟灰 2024-11-15 16:57:36

试试这个

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

Try this

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
if( fromIndexPath == toIndexPath ) {
return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文