如何将子视图添加到滚动视图的子视图

时间:2013-08-21 11:49:40

标签: ios uiscrollview

sample image

如何创建此视图。 用于添加滚动视图的代码

yPos=0;
for (int i=0; i<24; i++) {

    UIView *timeView=[[UIView alloc]initWithFrame:CGRectMake(71, yPos, 909, 60)];
    timeView.userInteractionEnabled=TRUE;
    timeView.exclusiveTouch=YES;
    if (i==4) {
        UIView *ssview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 80)];
        ssview.tag=1;
        ssview.userInteractionEnabled=TRUE;
        UILabel *recurenceId=[[UILabel alloc]init];
        recurenceId.text=@"A12334";

        [ssview addSubview:recurenceId];
        ssview.backgroundColor=[UIColor orangeColor];


        [timeView addSubview:ssview];
        [ssview addGestureRecognizer:tap];



    }

这里ssview高度超过timeview.so它也被添加到下一个子视图

这里每一行都是滚动视图的子视图。 现在我必须添加另一个绿色的视图。

2 个答案:

答案 0 :(得分:1)

NSArray *greenViewsIndexes=[NSArray arrayWithObjects:[NSNumber numberWithInt:5],[NSNumber numberWithInt:6],[NSNumber numberWithInt:11],[NSNumber numberWithInt:12], nil];   
for (int i=0; i<24; i++) {

for (int j=0; j>greenViews.count; j++) {
if ([[greenViews objectAtIndex:j]intValue]==i){
    UIView *greenView=[[UIView alloc]initWithFrame:CGRectMake(71,61*(j+1),100,80)];
    [greenView setBackgroundColor:[UIColor greenColor]];
    }
  }

  //another operations

}

答案 1 :(得分:0)

我遇到了你的问题。它不会与您的下一个单元格重叠,因为您的下一个单元格位于前一个单元格上并与前一个单元格的边缘重叠。尝试:从超级视图中删除单元格(将具有绿色视图),然后再次添加绿色视图:

NSArray *greenViewsIndexes=[NSArray arrayWithObjects:[NSNumber numberWithInt:5],[NSNumber numberWithInt:11], nil];   
for (int i=0; i<greenViewsIndexes.count; i++) {
int j=[[greenViewsIndexes objectAtIndex:i]intValue];
[[self.view subviews] objectAtIndex:i] removeFromSuperView]

UIView *timeView=[[UIView alloc]initWithFrame:CGRectMake(71, 61*(j+1), 909, 60)]; //set your values
timeView.userInteractionEnabled=TRUE;
timeView.exclusiveTouch=YES;
UIView *greenView=[[UIView alloc]initWithFrame:CGRectMake(71,61*(j+1),100,80)];  //set yourValues
[greenView setBackgroundColor:[UIColor greenColor]];
[timeView addSubview:greenView];
[self.view addSubview:timeView];
}