Titanium Mobile
  1. Titanium Mobile
  2. TIMOB-7828

Ti API: Problem with remove method to clear all children in a scrollview

    Details

    • Type: New Feature New Feature
    • Status: Open
    • Priority: Medium Medium
    • Resolution: Unresolved
    • Affects Version/s: Release 1.8.0.1
    • Fix Version/s: None
    • Component/s: TiAPI
    • Labels:
    • Environment:

      Titanium mobile sdk 1.8.1.0

      Description

      Hi,

      Lots of problem with remove method. Didn't find any function to clear all children.
      Create a simple scrollView (i guess it's the same for views), add some ImageViews inside and try :

      var simpleView = Ti.UI.createView({
      			id : 'mainView',
      			top : 0,
      			left : 0,
      			layout : 'absolute',
      			width : Ti.Platform.displayCaps.platformWidth,
      			height : Ti.Platform.displayCaps.platformHeight
      		});
      

      or

      var simpleView == Ti.UI.createScrollView({
      			top : 0,
      			zoomScale : 1,
      			minZoomScale : 1,
      			maxZoomScale : 2,
      			width : Ti.Platform.displayCaps.platformWidth,
      			height : Ti.Platform.displayCaps.platformHeight,
      			contentWidth : 'auto',
      			contentHeight : 'auto'
      }
      

      then ...

      var images = [];
      
      for(var i=0; i < 10; i++){
               images.push(Ti.UI.createImageView({
      					image : 'image' + i + '.jpg',
      					width : 600,
      					height : 400,
      					left : 0,
      					top : 0
      				}));
      }
      
      for(var i=0; i<imgs.length; i++){
          simpleView.add(images[i]);
      }
      
      
      var removeChildren = function(view){
      	while(view.children != undefined){
      		view.remove(view.children[0]);
      	}
      }
      
      removeChildren(simpleView);
      

      Getting sometimes :

      "*** -[__NSArrayM objectAtIndex:]: index N beyond bounds [0 .. X]";

      at this line :

      while(view.children != undefined)

      I tried other ways to do removeChildren method but none works well on titanium mobile 1.8.1.0 ( http://developer.appcelerator.com/question/131151/emptying-a-view#228486 )

      ********************

      Another method is the following :

      var removeAllChildren = function(viewObject) {
      	if(viewObject.children != undefined){
      		var children = viewObject.children.slice(0);
      		
      		for(var i = 0; i < children.length; i++) {
      			try {
      				viewObject.remove(children[i]);
      			} catch(e) {
      				Ti.API.error('removeAllChildren error : ' + e.message + ' ' + viewObject);
      			}
      		}
      		children = null;
      	}
      }
      

      but there's a warning on each remove invocation (in my case it's a scrollView instead of a view but i guess it's the same pb) :

      called remove for [object TiUIImageView] on [object TiUIScrollView], but [object TiUIImageView] isn't a child or has already been removed
      

      and sometimes i get

      "Result of expression 'viewObject.children' [undefined] is not an object.";

      for line

      if(viewObject.children != undefined)

      ********************

      • In general it's hard to loop through children of a view an to remove some children. This kind of way to do return sometimes an error :
      for(var i=0; i< simpleView.children.length; i++){
      	if(XXXXX){
      		simpleView.remove(player.ui.mainView.children[i]);
      		i--;
              }
      }
      

      at line

      for(var i=0; i< simpleView.children.length; i++)
      it launch sometimes this exception

      "*** -[__NSArrayM objectAtIndex:]: index X beyond bounds [0 .. X]";

      Best Regards

        Activity

          People

          • Assignee:
            Ingo Muschenetz
            Reporter:
            process
          • Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:

              Development