我在某处听说或读到出于性能原因应该避免使用 itemRendererFunction 但由于我找不到该评论的来源,是否可能是文档中的示例代码效率低下?我创建了一个我认为效率更高的替代代码示例(尚未进行测试)。
这是文档中的示例代码。请注意,在每次迭代中都会创建一个新的类工厂 [1]:
<fx:Script>
<![CDATA[
import myComponents.MySimpleItemRendererFunction;
import spark.skins.spark.DefaultItemRenderer;
private function selectRenderer(item:Object):ClassFactory {
var classFactory:ClassFactory;
if (item is String) {
// If the item is a String, use DefaultItemRenderer.
classFactory = new ClassFactory(DefaultItemRenderer);
}
else {
// If the item is an Object, use MySimpleItemRendererFunction.
classFactory = new ClassFactory(MySimpleItemRendererFunction);
}
return classFactory;
}
]]>
</fx:Script>
我的代码(未测试):
<fx:Script>
<![CDATA[
private var normalItemRendererFactory:ClassFactory = new ClassFactory(DefaultItemRenderer);
private var editableItemRendererFactory:ClassFactory = new ClassFactory(EditableItemRenderer);
private function itemRendererFunction(item:Object):ClassFactory {
if (item is String) {
// If the item is a String, use DefaultItemRenderer.
return normalItemRendererFactory;
}
else {
// If the item is an Object, use MySimpleItemRendererFunction.
return editableItemRendererFactory;
}
}
]]>
</fx:Script>