Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-7828] Ti API: Problem with remove method to clear all children in a scrollview

GitHub Issuen/a
TypeNew Feature
PriorityLow
StatusOpen
ResolutionUnresolved
Affected Version/sRelease 1.8.0.1
Fix Version/sn/a
ComponentsTiAPI
Labelscore
Reporterprocess
AssigneeUnknown
Created2012-02-24T05:25:26.000+0000
Updated2018-03-06T18:57:49.000+0000

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

Comments

  1. Ivan Skugor 2012-02-28

    "Result of expression 'viewObject.children' [undefined] is not an object." - you got that message because you passed "undefined" instead of TiView into that function. It can be avoided by modifying that line so it checks if value is falsy: "if(viewObject && viewObject.children != undefined)"
  2. process 2012-02-28

    I think there's a latency between the time when remove method is called and when view.length is updated

JSON Source