tags:

views:

1180

answers:

2

Hello i have a uiscrollview and i have one object of uiimageview inside the uiscrollview. I start the app. then i scroll to the right at the scrollview and the scrollview changes but i can not see the next uiimageview object that i add at - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView . before i add the next object i remove the previous...what is wrong?

to remove object i use

UIImageView *l;

    for (NSInteger ko=0; ko<[[scroll subviews] count]; ko++){
        if ([[[scroll subviews] objectAtIndex:0] isKindOfClass:[UIImageView class]]){

            //This code never gets hit
            l=[[scroll subviews] objectAtIndex:0];
            [l removeFromSuperview];
            l=nil;

        }
    }

then i add the next object

[scroll addSubview:imageView];

I remove the previous object because my app crashes when i add 110 images at the scrollview so i have to manage memory i guess. This is why i remove the previous object.

Anyone help please!

A: 

First problem I see is that you are looping over an array while also modifying it. Also your ko loop index is not used anywhere. You always grab item 0.

What about:

NSArray* subviews = [NSArray arrayWithArray: scroll.subviews];
for (UIView* view in subviews) {
    if ([view isKindOfClass [UIImageView class]]) {
         [view removeFromSuperview];
    }
}
[subviews release];

Isn't that exactly what you are trying to do?

St3fan
i do not want to remove all objects. There is an object at place 0 at the uiscrollview. i want to remove the first object because i want to add another. the new object should be putted at 1 place not at 0 place because i have already scroll from place 0 to place 1.If i do not remove the previous object after i add more than 45 images to the uiscrollview the app crashes because of memory problem. I have to remove the previous object to free memory.
stefanosn
The order of the subviews is really not relevant and you should not depend on it. There is no relation between the index of the subview in scroll.subviews and its actual position. If that is what you want then I suggest that you keep your own array of views too and make sure they are in display order.
St3fan
St3fan, don't release subviews. NSArray arrayWithArray: returns an autoreleased array. When your event loop exits and the release pool tries to release subviews, it will already be gone and your app will crash.
David Kanarek
David, I don't think that will happen. What my code will do is this: The subviews array (which is a *local* variable, not to be confused by *scroll.subviews*) will hold retained references to the items in scroll.subviews. It will ask those that are UIImageViews to remove itself from the superview (the scroll view). Then it will release the temporary array and that will make the `removed` subviews reach a retain count of 0, after which they will be released.The whole reason for doing it this way is that you cannot loop over scroll.subviews *and also modify it*. You need a temp array for that.
St3fan
A: 

A couple of suggestions to get you on the right track:

  1. If you believe that memory is an issue, partition up the problem. For example add/remove 10 images at a time.

  2. The tag property of a UIView is your best friend when trying to avoid maintaining problems with your own loops. You can set a specific tag for your UIImageViews when you need them, and then search for the tag using [UIView viewWithTag:tag]; It works recursively.

  3. st3fan brings up a very good point about concurrent modification. In general a good way to avoid it is to add to a "deletion array" and then removeObjectsInArray.

David Sowsy