how to add subview to subviews of scroll view

2019-08-29 07:04发布

问题:

how create this view. code for adding scroll view

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



    }

here ssview height is more than timeview.so it be added to next subview also

here every row is a subview of scroll view. Now i have to addd another view which is in green color.

回答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

}


回答2:

I got you problem. It's not overlap your next cell, because your next cell lie on your previous cell and overlap previous cell's edge. Try to: remove cell (which will has a green view) from super view and add again with green view:

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