在 iOS 表视图中,应如何将行移动到新部分? [英] In an iOS table view, how should a row be moved to a new section?

查看:24
本文介绍了在 iOS 表视图中,应如何将行移动到新部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表视图,在一个部分中有两行,即

I have a table view with two rows in one section, i.e

---
Row A
Row B

如果我想将第 1 行动画化到一个新部分,即最终结果是:

If I want to animate row 1 into a new section, i.e. the end result being:

---
Row B
---
Row A

这应该如何实现?我已经尝试过的是:

how should this be achieved? What I have tried already is:

[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow: 0 inSection: 0]] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView insertSections:[NSIndexSet indexSetWithIndex: 1] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

但是,至少在 iOS 8 中,该行会以动画形式显示出来,但随后新部分不会被渲染(即白色,没有底行边框),直到某个时间触发重绘并返回.

However, in iOS 8 at least, the row animates out but then the new section is un-rendered (i.e. white, no bottom row border) until some time after when something triggers a redraw and it comes back.

[self.tableView beginUpdates];
[self.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] toIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
[self.tableView endUpdates];

引发异常:

Invalid update: invalid number of sections. The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).

还有:

[self.tableView beginUpdates];
[self.tableView insertSections:[NSIndexSet indexSetWithIndex: 1] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] toIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
[self.tableView endUpdates];

引发异常:

cannot move a row into a newly inserted section (1) 

我是否应该根本不设置动画并重新加载整个表格?

Should I just not animate at all and reload the whole table?

推荐答案

您需要分两步执行此操作.

You'll need to perform this in two steps.

首先,您需要添加该部分;您需要告诉您的数据源考虑新的部分.对于像您这样的简单示例,您只需设置一个变量即可:

First you'll need to add the section; you need to tell your data source to account for the new section. For a simple example like yours, you can just set a variable:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if(hasTwoSections)
        return 2;

    return 1;
}

然后你想触发动画的地方,调用:

Then where you want to trigger the animation, call:

hasTwoSections = true;  // this needs to be set before endUpdates
[self.tableView beginUpdates];
[self.tableView insertSections:[NSIndexSet indexSetWithIndex: 1] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];

此时,tableView 将调用数据源方法根据您的更改更新表格.

At this point, the tableView will call the data source methods to update the table based on your changes.

添加完该部分后,您可以在另一个更新块中将该行移动到该部分:

Once the section has been added, you can move the row to it in another update block:

[self.tableView beginUpdates];
[self.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] toIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
[self.tableView endUpdates];

显然,您需要更新 numberOfRowsInSection 返回的值以说明更改.

Obviously you'll need to update what numberOfRowsInSection returns to account for the change.

虽然是两个步骤,但看起来像是同时发生的.

Although it's two steps, it will look like it's happening all at once.

这篇关于在 iOS 表视图中,应如何将行移动到新部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆