UIScrollView设置其contentOffset默认值?

时间:2013-08-16 11:12:18

标签: ios uiview uiscrollview

我有一个包含许多视图的滚动视图。这些视图根据其动态大小放置在视图中。每个视图上还有一个删除按钮,用于从滚动视图中删除视图,并相应地替换其他视图。

但我有一个问题,当我删除某些内容时,scrollview不会停留在我删除的位置。一行向上或向下。

这是我的删除代码:

-(void) redrawTheList:(id)sender :(NSString*) except{

int tempo = counterRow;

NSMutableDictionary *doctorTemp = [[NSMutableDictionary alloc]initWithDictionary:doctorAddition copyItems:YES];


for(int i = 0; i<=tempo;i++)
{
    NSString *dictTempKey = [NSString stringWithFormat:@"row%d",i];

  NSMutableArray * tempArray =  [doctorTemp objectForKey:dictTempKey];

    for(UIView * subview in tempArray)
    {
        for(UIView * subview2 in [subview subviews])
        {                
            if([subview2 isKindOfClass:[UILabel class]])
            {
               if([((UILabel*)subview2).text isEqualToString:except])
               {

                   counterRow = i;

                   tempile = i;

                   yAxis=5.0+(i*40.0);
               }
            }
        }
    }
}



    for(int j=tempile;j<=tempo;j++)
    {


        NSString *dictTempKey2 = [NSString stringWithFormat:@"row%d",j];

        NSMutableArray * tempArray =  [doctorAddition objectForKey:dictTempKey2];

          for(UIView * subview in tempArray)
          {
             [subview removeFromSuperview];
          }

             [doctorAddition removeObjectForKey:dictTempKey2];

    }

    for(int k=tempile;k<=tempo;k++)
    {
            NSString *dictTempKey3 = [NSString stringWithFormat:@"row%d",k];

            NSMutableArray * tempArray2 = [doctorTemp objectForKey:dictTempKey3];

              for(UIView * subview in tempArray2)
              {
                    for(UIView * subview2 in [subview subviews])
                    {
                       if([subview2 isKindOfClass:[UILabel class]])
                       {
                           if(![((UILabel*)subview2).text isEqualToString:except])
                           {

                               [self createDoctorBox:((UILabel*)subview2).text];

                           }
                       }
                    }
              }

    }    

}

CreateDoctorBox方法;

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


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


return doktorKutu;

}

1 个答案:

答案 0 :(得分:0)

因为当你的功能&#34; createDoctorBox&#34;调用,可能有更改了scrollview属性。请检查那里。

你的代码中的

[doctorList setContentSize:CGSizeMake(10,36 +(counterRow * 41))];它重置了UIScrollView的contentOffset。如果要保留删除按钮的位置,则在该行下方,将删除按钮指定为UIScrollView的contentOffset。 示例代码:

[doctorList setContentSize:CGSizeMake(10, 36+(counterRow*41))];
doctorList.contentOffset:CGMakePoint(0,deleteBtn.frame.origin.y);

如果有帮助,请投票给我。