2013-08-21 3 views
0

У меня есть scrollview, который содержит многие из UIView. UIView Объекты добавляются/удаляются в/из scrollview динамически и его contentSize также изменен.UIScrollView contenOffset и setContentSize?

Моя проблема в том, что изменения 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; 

} 

ответ

0

Я думаю, что вы можете использовать scrollRectToVisible: анимированный: Methoda к controll прокрутка scrollview вы можете прокручивать программно, чтобы исправить эту проблему.

@USE

[scrollView scrollRectToVisible:subview.frame animated:NO]; 
+0

теперь он это фиксируется в области и не прокручивается – erdemgc

+0

убедитесь, что ваш contentsize больше, то размер Scrollview – abhishekkharwar

+0

еще, я не в состоянии достичь своей цели, не scrollRectToVisible – erdemgc

Смежные вопросы