0

我有一个滚动视图,其中包含许多UIView. UIView对象被动态添加/删除,scrollview并且它contentSize也被更改。

我的问题是,如果setContentSize更改,它contentOffset会自动更改,并且当我从中删除某些内容时,scrollview它不会停留在删除的位置吗?

如何控制和发挥作用?

如果你问,我会添加代码。

当我删除某些内容时,使用此方法:

- (void) removeNameFromTheList:(id)sender{

//[doctorList setScrollEnabled:NO];


UIButton *x = sender;

            for(UIView *subview in [x.superview.superview subviews])
            {
                if([subview isKindOfClass:[UILabel class]])
                {
                    [self redrawTheList:sender:((UILabel*)subview).text];

                    NSMutableArray * tempo = [[NSMutableArray alloc]init];

                    tempo = addedList;

                    for(int i=0; i<tempo.count;i++)
                    {
                        if([(NSString*)[tempo objectAtIndex:i] isEqualToString:((UILabel*)subview).text])
                        {
                            [addedList removeObjectAtIndex:i];
                        }

                    }



                    NSArray *cells = [tableView visibleCells];

                    for (DoctorListCell *cell in cells)
                    {
                        UILabel *tempLabel = (UILabel*)subview;


                        if([cell.doctorName.text isEqualToString:tempLabel.text])
                        {                    
                            [cell.plusButton setHidden:NO];

                        }



                    }


                }
            }

            [x.superview.superview removeFromSuperview];


  NSLog(@"000:%f",doctorList.contentOffset.y);

  float temp = doctorList.contentOffset.y;



  [doctorList setContentSize:CGSizeMake(10, 41+(counterRow*41))];


  float temp2 = doctorList.contentOffset.y;


  [doctorList setContentOffset:CGPointMake(doctorList.contentOffset.x, doctorList.contentOffset.y+(temp2-temp))];


  NSLog(@"111: %f",doctorList.contentOffset.y);

当我创建某些东西时,会使用此方法:

-(UIView*)createDoctorBox : (NSString*)name {


[addedList addObject:name];

NSString *myString = name;

CGSize stringSize = [myString sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];


UIView *doktorKutu = [[UIView alloc]init];

[doctorList addSubview:doktorKutu];

UIView *doktorKutuBas = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 18, 36)];

[doktorKutuBas setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"doktor_isim_kutu_bas"]]];

[doktorKutu addSubview:doktorKutuBas];

UILabel * doktorKutuGovde = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, stringSize.width+3, 36)];

[doktorKutuGovde setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];

[doktorKutuGovde setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"doktor_isim_kutu_1px"]]];

[doktorKutuGovde setUserInteractionEnabled:YES];


[doktorKutuGovde setText:myString];

[doktorKutu addSubview:doktorKutuGovde];



UIView * doktorKutuKic = [[UIView alloc]initWithFrame:CGRectMake(stringSize.width+13, 0, 18, 36)];

[doktorKutuKic setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"doktor_isim_kutu_kic"]]];

[doktorKutu addSubview:doktorKutuKic];

UIImageView *cancelImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cikar"]];

cancelImage.frame = CGRectMake(-5,9, 18, 18);

[doktorKutuKic addSubview:cancelImage];

UIButton *cancel = [[UIButton alloc]initWithFrame:CGRectMake(-5,0, 20, 36)];

//[cancel setBackgroundImage:[UIImage imageNamed:@"cikar"] forState:UIControlStateNormal];

[cancel setUserInteractionEnabled:YES];

[cancel addTarget:self action:@selector(removeNameFromTheList:) forControlEvents:UIControlEventTouchUpInside];

[doktorKutuKic addSubview:cancel];

UITapGestureRecognizer *singlePress =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSinglePress:)];

[doktorKutuGovde addGestureRecognizer:singlePress];

[doktorKutu bringSubviewToFront:cancel];


[doctorCommented addObject:doktorKutu];

[doktorKutuKic bringSubviewToFront:cancel];

[doktorKutu bringSubviewToFront:cancel];

dictRowKey = [NSString stringWithFormat:@"row%d",counterRow];


NSMutableArray *row = [[NSMutableArray alloc]init];

doktorKutu.frame = CGRectMake(5, yAxis, stringSize.width+30, 36);


if([doctorAddition objectForKey:dictRowKey]!=nil)
{
    row = (NSMutableArray*)[doctorAddition objectForKey:dictRowKey];

    if(row.count>0)
    {
        int totalWidth = 5;

        for(int i=0;i<row.count;i++)
        {
                totalWidth = totalWidth + ((UIView*)[row objectAtIndex:i]).frame.size.width+5;
        }

        if(totalWidth+stringSize.width<520)
        {
            doktorKutu.frame = CGRectMake(totalWidth, yAxis, stringSize.width+30, 36);

            [row addObject:doktorKutu];

            [doctorAddition removeObjectForKey:dictRowKey];

            [doctorAddition setObject:row forKey:dictRowKey];
        }
        else
        {
            doktorKutu.frame = CGRectMake(5, yAxis+40, stringSize.width+30, 36);

            yAxis= yAxis + 40.0;
            counterRow++;
            dictRowKey = [NSString stringWithFormat:@"row%d",counterRow];

            row = [[NSMutableArray alloc]init];

            [row addObject:doktorKutu];

            [doctorAddition setObject:row forKey:dictRowKey];



        }

    }

}
else
{
    [row addObject:doktorKutu];

    [doctorAddition setObject:row forKey:dictRowKey];
}    

float temp = doctorList.contentOffset.y;



[doctorList setContentSize:CGSizeMake(10, 41+(counterRow*41))];


float temp2 = doctorList.contentOffset.y;


[doctorList setContentOffset:CGPointMake(doctorList.contentOffset.x, doctorList.contentOffset.y+(temp2-temp))];


if(counterRow>3)
{

    CGPoint bottomOffset = CGPointMake(0, doctorList.contentSize.height - doctorList.bounds.size.height);
    [doctorList setContentOffset:bottomOffset animated:YES];
}



return doktorKutu;

}
4

1 回答 1

0

我认为您可以使用scrollRectToVisible:animated: Methoda 来控制滚动视图的滚动,您可以通过编程方式滚动来解决此问题。

@利用

[scrollView scrollRectToVisible:subview.frame animated:NO];
于 2013-08-21T07:13:56.040 回答