2016-06-29 3 views
0

を修正するためにスクロールしませんでしたUITableViewScrollPositionMiddle` `へ:iOS版:設定scrollPositionそれはこれが以下の私のコードである位置

if (![self.cityLabel.text isEqualToString:@""] && dataOfCity.count != 0) { 
     for (int i = 0; i < dataOfCity.count; i ++) { 

      if ([dataOfCity[i] isEqualToString:self.cityLabel.text]) { 

       NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 
       [_tab_city selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; 
      } 
     } 

    } 

そしてresult以下のtableViewの真ん中ではありません写真です:

The result picture

答えて

0

回答:私は私の質問の理由を発見した

それは私がcompletion blockにそれらのコードを置き換え、その問題が解決している間違った場所、 が表示されますので、私は、uiview animation前にそのコードを実行します。

- (void)show{ 

    if (self.isOpenState == YES) { 
     return; 
    } 



    [UIView animateWithDuration:0.3 animations:^{ 

     //CGRect frame = self.backOfTabAndTrailer.frame; 
     self.opacity_back.alpha = 0.2; 

     //frame.size.height = _rowHeight * _numberOfRow; 
     //[self.backOfTabAndTrailer setFrame:frame]; 

     self.backOfTabAndTrailer.frame = CGRectMake(_backOfTabAndTrailer.frame.origin.x, _backOfTabAndTrailer.frame.origin.y, _backOfTabAndTrailer.frame.size.width, _rowHeight * _numberOfRow); 
     self.tab_province.frame = CGRectMake(self.tab_province.frame.origin.x, self.tab_province.frame.origin.y, self.tab_province.frame.size.width, _rowHeight * _numberOfRow); 
     self.tab_city.frame = CGRectMake(self.tab_city.frame.origin.x, self.tab_city.frame.origin.y, self.tab_city.frame.size.width, _rowHeight * _numberOfRow); 
     self.tab_country.frame = CGRectMake(self.tab_country.frame.origin.x, self.tab_country.frame.origin.y, self.tab_country.frame.size.width, _rowHeight * _numberOfRow); 
     self.trailerView.frame = CGRectMake(_backOfTabAndTrailer.frame.origin.x, _rowHeight * _numberOfRow, self.trailerView.frame.size.width, Height_Of_Trailer); 
     self.trailerImage.frame = CGRectMake(_allFrame.size.width/2 - Height_Of_Trailer/2, 0, Height_Of_Trailer, Height_Of_Trailer); 
     self.trail_line.frame = CGRectMake(self.backOfTabAndTrailer.frame.origin.x, 0, self.trail_line.frame.size.width, 1); 
     self.trailerButton.frame = CGRectMake(self.backOfTabAndTrailer.frame.origin.x, 0, self.trailerButton.frame.size.width, 1); 

    } completion:^(BOOL finished){ 

     self.isOpenState = YES; 

     /* 让tabview滚到被选中的地方 */ 
     if (![self.provinceLabel.text isEqualToString:@""] && dataOfProvince.count != 0) { 
      for (int i = 0; i < dataOfProvince.count; i ++) { 

       if ([dataOfProvince[i] isEqualToString:self.provinceLabel.text]) { 

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 
        [_tab_province selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; 
       } 
      } 

     } 
     if (![self.cityLabel.text isEqualToString:@""] && dataOfCity.count != 0) { 
      for (int i = 0; i < dataOfCity.count; i ++) { 

       if ([dataOfCity[i] isEqualToString:self.cityLabel.text]) { 

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 
        [_tab_city selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; 
       } 
      } 

     } 
     if (![self.countryLabel.text isEqualToString:@""] && dataOfCountry.count != 0) { 
      for (int i = 0; i < dataOfCountry.count; i ++) { 

       if ([dataOfCountry[i] isEqualToString:self.countryLabel.text]) { 

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 
        [_tab_country selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; 
       } 
      } 

     } 

    }]; 



} 
1
I think I understand what happens: 
the table view is already performing another animation and the scrollToRowAtIndexPath is ignored. 
Wrapping the method in CATransaction block works for me (swift code): 

    CATransaction.begin() 

    tableView.beginUpdates() 
    tableView.scrollToRowAtIndexPath(cell.indexPath, atScrollPosition: .Middle, animated: true) 
    tableView.endUpdates() 

    CATransaction.commit() 

    Another solution that worked for me was using performSelectorWithDelay, with the delay being long enough for other animations on the table view to finish. 
関連する問題