Reputation: 729
I have created one scroll view and on the scroll view, i am adding two alternate custom UIView s
@interface FirstView : UIView
@interface SecondView : UIView
my array is
NSMutableArray *classArray= [[NSMutableArray alloc] initWithObjects:firstView, secondView, firstView, secondView, nil];
i am adding views on scroll view in following manner
for ( i = 0 ; i< 5; i++)
{
UIView *userView = [classArray objectAtIndex:i];
CGRect rect;
rect.size.height = KVIEWHGT;
float userViewWidth = userView.frame.size.width;
rect.size.width = userViewWidth;
userView.frame = rect;
[scrollView addSubview:userView];
[userView release];
}
UIView *userView = nil;
NSArray *userSubViews = [scrollView subviews];
CGFloat x = 0;
float userViewWidth = 0.0f;
for (userView in userSubViews) {
if ([userView isKindOfClass:[UIView class]]){
CGRect frame = userView.frame;
frame.origin = CGPointMake(x, 0);
userView.frame = frame;
userViewWidth = userView.frame.size.width;
x += userViewWidth + 10;
}
}
[scrollView setContentSize:CGSizeMake(x, scrollView.frame.size.height)];
I am getting output as only two custom views on scroll view but i want there should be 5 custom view on the scroll views
Please help me to solve this problem.
Upvotes: 0
Views: 224
Reputation: 1145
even if it works, you would have duplicated views in your scrollview. you have to create x instances of you views and add them to your scrollview.
thats kind of creepy code anyway.
1.
UIView *userView = [classArray objectAtIndex:i];
[userView release];
you dont have to release this, because its not yours!
2: why iterating twice over the views? makes no sense, better
NSMutableArray* myArray = [[NSMutableArray alloc] initWithObjects: firstView,secView,thirdView,fourthView];
for (UIView* view in myArray)
{
//set the Frame and add it the the ScrollView
}
Upvotes: 0
Reputation: 15296
You are adding firstView ,secondView multiple time in the array, even though you add it multiple time it refers to the same instance
NSMutableArray *classArray= [[NSMutableArray alloc] initWithObjects:firstView, secondView, firstView, secondView, nil];
Upvotes: 1
Reputation: 26952
A UIView can only have one parent view. You are using the same UIView instance multiple times. If you add a UIView to another, it is removed from the one it is currently attached to.
Read UIView Class Reference, addSubview:
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.
Upvotes: 0