【问题标题】:Trouble with reloadSections:withRowAnimation animationreloadSections:withRowAnimation 动画出现问题
【发布时间】:2012-05-15 02:37:01
【问题描述】:

我有一个包含两个部分(顶部和底部)的 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];

我应该如何编写代码以获得流畅的动画?

编辑:

我发现了问题。应该这样写:

//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];

注意我注释掉的第一行。我最初忽略了添加这个。如您所见,我使用的是构造不佳的 NSIndexSet。

【问题讨论】:

    标签: ios xcode uitableview reloaddata


    【解决方案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];
    

    【讨论】:

    • 这是正确答案。不明白为什么会被[tableView reloadData]自动触发。这不应该只是一个可以被覆盖的函数吗?
    • 老兄,如果您调用 reloadData,那么在它之前执行任何“reloadSections”之类的所有代码都会变得毫无用处。 ReloadData 重新加载一切。因此,您要么执行 reloadData,要么执行 reloadSections/reloadRows/etc 来挑选要重新加载表的哪些部分,而不是两者兼而有之。
    【解决方案2】:

    为什么你需要在函数结束时重新加载你的 guestList 的数据?您已经在重新加载您想要的部分...尝试删除它并再次检查。

    【讨论】:

    • 谢谢。在发布之前,我实际上在没有 reloadData 的情况下尝试过它。没有它,表格视图将无法正确更新。您是否应该与 reloadSections 一起使用其他代码?我看不懂这个动画,也没有在搜索中找到任何有用的东西。
    猜你喜欢
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2020-01-10
    • 1970-01-01
    相关资源
    最近更新 更多