Search code examples
viewtitaniumappceleratorscrollable

clear views in ScrollableView


i have a titanium project with alloy. Then in a screen i have a ScrollableView. When i open this screen i need remove all views of a screen, but i can't.

principal.xml

<ScrollableView id="EmisorView" onScrollend="CambioEmisor" top="10%"  height="10%" width="100%" backgroundColor="#fff">
</ScrollableView>    

principal.js

function ConstruyoEmisores(){

var db = Ti.Database.open('Termolink');
var rows = db.execute('SELECT * FROM Regulaciones ORDER BY Regulaciones.Nombre,Regulaciones.Serie');

NRegistros=ComprueboRegBD();

for (i=0;i<$.EmisorView.views.length;i++){
    $.EmisorView.removeView($.EmisorView.views[i]);
}

var i;

var serieenqueestoy=0;

for (i=0;i<NRegistros;i++){
    TablaNombreTermostatos[i]= rows.field(2);

    if (rows.field(0)==Serie) serieenqueestoy=i; //esto es para posicionar en el seleccionado

    var nuevaView=Ti.UI.createView();   

    var titulo1=Ti.UI.createLabel({
        id: "NombreTerm",
        text: TablaNombreTermostatos[i],
        color: "#b0acb1" ,
        textAlign: Ti.UI.TEXT_ALIGNMENT_LEFT, 
        left:"44%",
        top:"33%"
    });
    var imagen1=Ti.UI.createImageView({
        id: "Radiador",         
        image: "/figura radiador.png",
        height: "60%",
        left:"32%",
        top:"20%"
    });
    if (Ti.Platform.name=="iPhone OS") {
        imagen1.left = "13%";
    }

    nuevaView.add(imagen1);
    nuevaView.add(titulo1);                         

    $.EmisorView.addView(nuevaView);

    rows.next();
}       

db.close(); 

$.EmisorView.scrollToView(serieenqueestoy);

}

When i run the first time all is ok, but in another times the ScrollableView have more and more views.

Any suggestions about clear the scrollableView??

I work with titanium 5.3.1, alloy, Android and IOS.

NEW VERSION. DON'T RUN

$.EmisorView.cleanup = function() {

$.destroy();
$.off();
//null your objects here

};

function ConstruyoEmisores(){

var db = Ti.Database.open('Termolink');
var rows = db.execute('SELECT * FROM Regulaciones ORDER BY Regulaciones.Nombre,Regulaciones.Serie');

NRegistros=ComprueboRegBD();

var CuantosViews=$.EmisorView.views.length;

for (i=0;i<CuantosViews;i++){
    $.EmisorView.views[parseInt(parseInt(i))].cleanup && $.EmisorView.views[parseInt(parseInt(i))].cleanup();
    $.EmisorView.removeView($.EmisorView.views[i]);
    $.EmisorView.views[i]=null;
}

var i;

var serieenqueestoy=0;

for (i=0;i<NRegistros;i++){
    TablaSerieTermostatos[i]=rows.field(0);
    TablaPinTermostatos[i]=rows.field(1);
    TablaNombreTermostatos[i]= rows.field(2);
    TablaTemperTermostatos[i]= rows.field(345);
    TablaConsignaTermostatos[i]= rows.field(5);
    TablaEstadoTermostatos[i]= rows.field(344);

    if (rows.field(0)==Serie) serieenqueestoy=i; //esto es para posicionar en el seleccionado

    var nuevaView=Ti.UI.createView();   

    var titulo1=Ti.UI.createLabel({
        id: "NombreTerm",
        text: TablaNombreTermostatos[i],
        color: "#b0acb1" ,
        textAlign: Ti.UI.TEXT_ALIGNMENT_LEFT, 
        left:"44%",
        top:"33%"
    });
    var imagen1=Ti.UI.createImageView({
        id: "Radiador",         
        image: "/figura radiador.png",
        height: "60%",
        left:"32%",
        top:"20%"
    });
    if (Ti.Platform.name=="iPhone OS") {
        imagen1.left = "13%";
    }

    nuevaView.add(imagen1);
    nuevaView.add(titulo1);                         

    $.EmisorView.addView(nuevaView);
    //$.EmisorView.insertViewsAt(i,nuevaView);

    rows.next();
}       

db.close(); 
$.EmisorView.scrollToView(serieenqueestoy);     
}

DEFINITIVE VERSION, NOW RUN

var CuantosViews=$.EmisorView.views.length;

for (i= CuantosViews;i>0;i--){
   $.EmisorView.removeView($.EmisorView.views[i-1]);
   $.EmisorView.views[i-1]=null;
}

Thanks all the people. The problem is this loop must be from the end to the start. Then remove all the views fine.


Solution

  • If you are trying to remove the ScrollableViews, you don't use the remove() or the removeAllChildren() methods, but the removeView() method.

    This is how you can remove the ScrollableView views, and cleanup them:

    if($.scrollableview.views && parseInt($.scrollableview.views.length)) for(var i = parseInt($.scrollableview.views.length); i > 0; i--) if($.scrollableview.views[parseInt(parseInt(i)-1)]) {
    
        $.scrollableview.views[parseInt(parseInt(i)-1)].cleanup && $.scrollableview.views[parseInt(parseInt(i)-1)].cleanup();
        $.scrollableview.removeView($.scrollableview.views[parseInt(parseInt(i)-1)]);
        $.scrollableview.views[parseInt(parseInt(i)-1)] = null;
    }
    

    This is an example of a View: view.xml

    <View id="view">
        <Label id="title"/>
    </View>
    

    view.js

    $.title.text = 'test';
    
    $.view.cleanup = function() {
    
        $.destroy();
        $.off();
        //null your objects here
    };