Reputation: 25132
I have a ScrollView
which I am adding multiple View
's into using this code:
payload = {
booking : b,
duration: duration,
removeBooking: function(view_to_remove) {
removeBooking(view_to_remove);
},
updateBooking: function(view_to_remove, new_booking) {
removeBooking(view_to_remove);
overlayBooking(new_booking);
}
};
booking = Alloy.createController('appointments_bookable_booking', payload).getView(); // returns a View
$.intervals_container.add(booking); // this is adding a View into a ScrollView
...
removeBooking()
then gets executed later on as a callback. I am then trying to remove the single View
like this:
function removeBooking(booking_view) {
for (var i = $.intervals_container.children.length - 1; i >= 0; i--) {
if(booking_view === $.intervals_container.children[i]) {
Ti.API.info('Found a match');
$.intervals_container.remove($.intervals_container.children[i]);
}
};
}
I'm looping through the children just to show the view I'm trying to remove DOES actually exist in the children views array. I do get the 'Found a match' printed to my console but the view is never actually removed.
There are other views being added into the ScrollView
as well. If I loop through the intervals_container.children
and remove each one. The other views are removed apart from the ones created using the code above.
Can anyone see why I can't remove the view?
UPDATE
After trying ericfreshz'a answer I can remove ALL the views however this is not working to remove one:
if ($.intervals_container.children) {
var _viewCount = $.intervals_container.children.length;
for ( s = 0; s < _viewCount; s++) {
if($.intervals_container.children[((_viewCount - 1) - s)] === booking_view) {
Ti.API.info('Removing view');
$.intervals_container.remove($.intervals_container.children[((_viewCount - 1) - s)]);
}
}
}
This should only remove the view which has been passed into the function but it doesn't. I get "Removing view" in my console but the view isn't removed.
UPDATE 2
I did an experiment to see if the scrollview was actually changed and it turns out the view is actually removed as the number of children views actually decreases however the view is still visible.
Ti.API.info('Count BEFORE: ' + $.intervals_container.children.length);
$.intervals_container.remove(booking_view);
Ti.API.info('Count AFTER: ' + $.intervals_container.children.length);
The result is one less.
Upvotes: 0
Views: 731
Reputation: 25
First is why not simply call
$.intervals_container.remove(booking_view);
I dont think you need to go through the hassle of children and children length. That should work If it doesn't can you try changing the scrollview layout (height, width,left...) and see if that changes something? (might be a layout update issue).
As a side note you should hold a reference of "children". Every time you access it it needs to make a copy natively. Plus there is a lock there so it might get long.
Upvotes: 0
Reputation: 1
Let's try my code as below :
if ($.intervals_container.children) {
var _viewCount = $.intervals_container.children.length;
for ( s = 0; s < _viewCount; s++) {
$.intervals_container.remove($.intervals_container.children[((_viewCount - 1) - s)]);
}
}
Upvotes: 0