i have a problem using the itemRenderer functionality. When using an ArrayCollection the visible Data in the 开发者_运维知识库DataGrid using the itemRenderer will be rendered just fine. But if i start scrolling the entries are repeating in the cells using the renderer. The cells are not filled with date according to the id. What mistake i'm doing here.
I read a lot of the explainations like:
http://blogs.adobe.com/aharui/2007/03/thinking_about_item_renderers_1.html
here is the code for the set data function (itemRenderer is extending HBox):
override public function set data(value:Object):void {
_data = value;
if(data!=null)
{
var maxValue:Number = 0;
var maxFontHeight:int = 18;
for each(var term:ArrayCollection in _data.story)
{
if((term.getItemAt(1) as Number)>maxValue)
maxValue=term.getItemAt(1) as Number;
}
for each(var term:ArrayCollection in _data.story)
{
var FontHeight:int = Math.floor((term.getItemAt(1) as Number) * maxFontHeight / maxValue);
var l:Label = new Label();
l.text = term.getItemAt(0) as String;
l.setStyle("fontWeight","normal");
l.setStyle("fontFamily","Verdana");
l.setStyle("paddingRight",0);
l.setStyle("paddingLeft",0);
l.setStyle("fontSize", FontHeight);
l.setStyle("color", 0x000000);
this.addChild(l);
}
}
}
You aren't clearing down what's already there in the renderer before adding new stuff.
Try moving the construction of the label to createChildren, setting the text in set data (rather than constructing another label), and remembering to clear the text if the data is null.
There's a few more optimizations you could make such as checking the new data isn't the same as the current data before doing the work, for example.
精彩评论