ArrayCollection.setItemAt 正在做一些有趣的事情

发布于 2024-09-27 08:29:38 字数 513 浏览 9 评论 0原文

我正在尝试使用此代码交换 ArrayCollection 中的两个项目。

private function swapCollectionElements(collection:ArrayCollection, fromIndex:uint, toIndex:uint) : void 
{
    var curItem:Object = collection.getItemAt(fromIndex);
    var swapItem:Object = collection.getItemAt(toIndex);

    collection.setItemAt(curItem, toIndex);
    collection.setItemAt(swapItem, fromIndex);

    collection.refresh();
}

调试代码时,我可以看到 curItem 和 swapItem 是正确的对象,但是当我执行第一个 setItemAt 时,它替换了我想要的对象,但也替换了我不想要的对象。有什么想法吗?

I am trying to swap two items in an ArrayCollection with this code.

private function swapCollectionElements(collection:ArrayCollection, fromIndex:uint, toIndex:uint) : void 
{
    var curItem:Object = collection.getItemAt(fromIndex);
    var swapItem:Object = collection.getItemAt(toIndex);

    collection.setItemAt(curItem, toIndex);
    collection.setItemAt(swapItem, fromIndex);

    collection.refresh();
}

When debugging the code I can see that curItem and swapItem are the correct objects, but when I do my first setItemAt, it replaces the one I wanted but also one that I didnt want. Any ideas what is going on here?

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

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

发布评论

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

评论(1

日暮斜阳 2024-10-04 08:29:38

这是因为调用 getItemAt 来设置 curItem 和 swapItem 会导致引用 ArrayCollection 中的对象而不是对象本身。当您使用第一个 setItemAt 更改对象时,您的引用也会更改。此时 curItem 和 swapItem 可能都引用同一个对象。我会以不同的方式处理这个问题,并使用removeItemAt和addItemAt来代替,这样您就可以使用对象而不是引用。希望有帮助。

This is because calling getItemAt to set curItem and swapItem results in references to the objects in the ArrayCollection rather than the objects themselves. When you change the object with your first setItemAt, your reference changes as well. At that point both curItem and swapItem probably refer to the same object. I would approach this differently and use removeItemAt and addItemAt instead, that way you are working with the objects rather than references. Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文