0

このコードで ArrayCollection 内の 2 つの項目を交換しようとしています。

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 を実行すると、必要なオブジェクトだけでなく、不要なオブジェクトも置き換えられます。ここで何が起こっているのですか?

4

1 に答える 1

4

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.

于 2010-10-11T21:50:09.923 に答える