reloadSections的麻烦:withRowAnimation动画

myS*_*ril 4 xcode uitableview reloaddata ios

我有一个UITableView有两个部分(顶部和底部).当在顶部(第0部分)"检查"项目时,我将它们移动到底部(第1部分),反之亦然.除了动画,一切都很好.

我正在使用以下内容,但行动作缓慢 - 我在其他应用程序中看到了更好的结果.我希望从顶部开始的行能够干净地动画到底部...以及从底部开始的行,以便在选中或取消选中它们时干净地动画到顶部.

// set the guests arrival status and use animation
    [guestList beginUpdates];
    if (!guest.didArrive) {
        [guest setDidArrive:YES];
        [guestList reloadSections:sectionIndexSet withRowAnimation:UITableViewRowAnimationBottom];
    } else {
        [guest setDidArrive:NO];
        [guestList reloadSections:sectionIndexSet withRowAnimation:UITableViewRowAnimationTop];
    }
    [guestList endUpdates];

[guestList reloadData];
Run Code Online (Sandbox Code Playgroud)

我该怎么编码才能获得流畅的动画?

编辑:

我发现了这个问题.应该用这种方式编写:

//NSIndexSet *sectionIndexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)];

    // set the guests arrival status and use animation
    [guestList beginUpdates];
    if (!guest.didArrive) {
        [guest setDidArrive:YES];
        [guestList reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationBottom];
    } else {
        [guest setDidArrive:NO];
        [guestList reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationTop];
    }
    [guestList endUpdates];
[guestList reloadData];
Run Code Online (Sandbox Code Playgroud)

注意我注释掉的第一行.我原本忽略了这个.如您所见,我使用的是构造不良的NSIndexSet.

myS*_*ril 7

问题解决了.请参阅已编辑的回复.代码应该是:

// set the guests arrival status and use animation
[guestList beginUpdates];
if (!guest.didArrive) {
    [guest setDidArrive:YES];
    [guestList reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationBottom];
} else {
    [guest setDidArrive:NO];
    [guestList reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationTop];
}
[guestList endUpdates];
[guestList reloadData];
Run Code Online (Sandbox Code Playgroud)

  • 老兄,如果你调用reloadData,它之前的所有代码都会执行任何"reloadSections",这样的东西就变得无用了.ReloadData重新加载所有内容.所以你要做一个reloadData,或者你做reloadSections/reloadRows/etc来挑选你要重新加载的表的哪些部分,而不是两者. (11认同)