ArrayCollection.setItemAt 正在做一些有趣的事情
我正在尝试使用此代码交换 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为调用 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.