2016-01-07 71 views
0

我有tableView和其中一個行我有一個水平scrollView和一些圖像(圖像是在不同的視圖行),在水平scrollView我添加了幾個按鈕(10個按鈕)。當用戶點擊scrollView中的任何按鈕時,我打電話給reloadData。因爲我打電話reloadData所以水平scrollView重新加載與新的圖像數據。現在我想如果用戶滾動結束並單擊scrollView中的最後一個按鈕,用戶應該能夠看到所選按鈕不是第一個按鈕。我不知道如何使用scrollRectToVisible:animated來實現此目的。如何使用scrollRectToVisible:動畫?

這裏是我的代碼爲scrollView和添加按鈕。

-(void)addColorFiltersOnView:(UIView *)colorView{  
    if (_colorFilterView) { 
     [_colorFilterView removeFromSuperview]; 
    } 

    self.colorFilterView = [[UIScrollView alloc] initWithFrame:colorView.bounds]; 
    [_colorFilterView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight]; 
    [_colorFilterView setBackgroundColor:samergb(238)]; 
    [_colorFilterView setShowsHorizontalScrollIndicator:NO]; 

    int addedCount = 0; 
    int i=0; 

    CGFloat x = 10, y = 5; 

    if ([_colorFilters count] < 1) { 
     return; 
    } 

    for (i=0; i<[_colorFilters count] - 1; i++) { 

     id item = [_colorFilters objectAtIndex:i]; 

     if ([item isKindOfClass:[NSString class]]){ 

      if ([self.prodVIPDictionary.PDcolor isEqualToString:(NSString*)item] && i != 0) { 
       continue; 
      } 

      NSInteger itemsCount = [[_colorFilters objectAtIndex:i+1] integerValue]; 

      if (itemsCount <= 0) { 
       break; 
      } 

      TTTAttributedLabel *itemLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero]; 
      [itemLabel setNumberOfLines:1]; 
      [itemLabel setTextAlignment:NSTextAlignmentCenter]; 
      [itemLabel setFont:[UIFont systemFontOfSize:14]]; 

      if ([_selectedColor isEqualToString:(NSString*)item]) 
       [itemLabel setTextColor:[UIColor whiteColor]]; 

      [itemLabel setText:[(NSString*)item capitalizedString]]; 
      [itemLabel setBackgroundColor:samergb(255)]; 
      itemLabel.layer.borderWidth = 1; 
      itemLabel.layer.masksToBounds = YES; 
      itemLabel.layer.cornerRadius = 10; 
      itemLabel.layer.borderColor = samergb(210).CGColor; 
      [itemLabel sizeToFit]; 
      [_colorFilterView addSubview:itemLabel]; 

      CGRect itemFrame = itemLabel.frame; 
      itemFrame.size.width += 20; 
      [itemLabel setFrame:itemFrame]; 

      UIButton *optionBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, itemLabel.frame.size.width + 6, 26)]; 
      [optionBtn setTag:[_colorFilters indexOfObject:item]]; 
      [optionBtn addTarget:self action:@selector(optionColorClicked:) forControlEvents:UIControlEventTouchUpInside]; 

      [_colorFilterView addSubview:optionBtn]; 

      [itemLabel setFrame:optionBtn.frame]; 

      if ([_selectedColor isEqualToString:(NSString*)item]) { 
       [optionBtn setEnabled:NO]; 
       [itemLabel setBackgroundColor:[LRUtils lrGreenColor]]; 
      } 

      addedCount ++; 

      x += optionBtn.frame.size.width+5; 

      if (addedCount >= 20) { 
       break; 
      } 

     }else{ 
      continue; 
     } 
    } 

    [_colorFilterView setContentSize:CGSizeMake((x + 10),colorView.frame.size.height)]; 
    [colorView addSubview:_colorFilterView]; 
} 
+0

這不是很清楚。如果用戶滾動並點擊最後一個按鈕,與您想要發生的事情相比,實際發生了什麼?由於某種原因,滾動視圖是否回到起點?也許你應該用你的'optionColorClicked:'方法更新你的問題。 – rmaddy

+0

@rmaddy我剛剛更新了我的問題。 – Zac24

回答

0

與其說reloadData,你可以呼籲各表視圖這種方法:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

您將需要創建一個包含所有IndexPaths的表視圖除了NSArray對於具有滾動視圖和按鈕的行。這樣,滾動視圖不會被重置。

+0

這是我曾經想過但沒有嘗試的一個選項,因爲scrollView和Image數據在同一個單元中,所以即使我使用reloadRowsAtIndexPath,ScrollView也會重置。我認爲沒有其他的方式,除了在一個單獨的單元格中創建scrollView。 – Zac24