0

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

但是我有一个问题,当我删除某些内容时,滚动视图不会停留在我删除的位置。向上或向下一排。

这是我的删除代码:

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

}
4

1 回答 1

0

这是因为当您的函数“createDoctorBox”调用时,您可能已经更改了滚动视图的属性。请检查那里。

在您的代码中 [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);

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

于 2013-08-16T11:22:38.310 回答