2011-12-21 94 views
0

我已创建了一个滚动视图和上滚动视图滚动视图,我加入两个交替的自定义的UIView小号添加视图到

@interface FirstView : UIView 
@interface SecondView : UIView 

我的阵列是

NSMutableArray *classArray= [[NSMutableArray alloc] initWithObjects:firstView, secondView, firstView, secondView, nil]; 

我在添加视图在下面的方式滚动视图

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)]; 

我得到输出上只有两个滚动视图定制的意见,但我想有应该是滚动视图上的5个自定义视图

请帮我解决这个问题。

回答

1

您要添加的firstView,secondView多时间在阵列中,即使你加它,它指的是同一个实例

NSMutableArray *classArray= [[NSMutableArray alloc] initWithObjects:firstView, secondView, firstView, secondView, nil]; 
0

UIView只能有一个父视图。您多次使用相同的UIView实例。如果你添加一个UIView到另一个,它会从它当前连接的那个中移除。

读取UIView类参考,addSubview:

视图只能有一个上海华。如果视图已经有一个超级视图,并且该视图不是接收者,则在使接收者成为新的超级视图之前,此方法将删除先前的超级视图。

+0

我加入的UIScrollView的 – 2011-12-21 09:56:42

+0

我知道所有的UIView,而是看你的阵列 “[[NSMutableArray的页头] initWithObjects:的firstView,secondView,的firstView,secondView,零]。”您多次使用相同的实例。您需要为屏幕上的每个UIView创建唯一的UIView实例,它们可以具有相同的类型/类。这意味着* view1 = [FirstView alloc] init],* view2 = [[FirstView alloc] init],* view3 = [[SecondView alloc] init],* view4 = [[SecondView alloc] init] .... etc – bandejapaisa 2011-12-21 10:01:31

0

即使工作多的时候,你就会有重复的观点您滚动视图。您必须创建视图的x个实例并将其添加到您的滚动视图中。

这是一种令人毛骨悚然的代码。

1.

UIView *userView = [classArray objectAtIndex:i]; 
[userView release]; 

你没有释放这一点,因为它不是你的!

2:为什么在视图上迭代两次?是没有意义的,更好的

NSMutableArray* myArray = [[NSMutableArray alloc] initWithObjects: firstView,secView,thirdView,fourthView]; 

for (UIView* view in myArray) 
{ 
    //set the Frame and add it the the ScrollView 
}