2015-11-03 35 views
0

我已將UIProgressView置於UITableViewCell中。我面臨兩個問題:
1. UITableView滾動正在受到阻礙。
2. UIProgressViewsetProgress每次被調用(顯然),每次都會調用它的進度。將UIProgressView放入UITableViewCell中時發生的問題

可以理解的是問題1發生是因爲問題2。爲了測試這個,我在UITableViewCell中評論UIProgressView。當時的卷軸非常快。
這是UIProgressView代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     static NSString *CellIdentifier = @"Cell"; 

     SWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) 
     { 
      cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
      cell.backgroundColor = [UIColor clearColor]; 

      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      cell.accessoryType = UITableViewCellAccessoryNone; 
      if ([cell respondsToSelector:@selector(layoutMargins)]) { 
       cell.layoutMargins = UIEdgeInsetsZero; 
       cell.preservesSuperviewLayoutMargins = false; 
      } 
     } 
     else 
     { 
      NSArray *arr = [cell.contentView subviews]; 
      for(int i=0; i<[arr count]; i++) 
      { 
       UIView *view = [arr objectAtIndex:i]; 
       [view removeFromSuperview]; 
      } 
     } 

CustomerArticleDetail *customerArticleDetails = [arrDetails objectAtIndex:indexPath.row]; 
... 
... 
UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; 
      [progressView setProgressTintColor:[UIColor colorWithRed:31.0/255 green:177.0/255 blue:137.0/255 alpha:1.0]]; 
      [[progressView layer]setCornerRadius:5.0f]; 
      [[progressView layer]setBorderWidth:2.0f]; 
      [[progressView layer]setMasksToBounds:TRUE]; 
      progressView.clipsToBounds = YES; 
      [[progressView layer]setFrame:CGRectMake(50, 93, self.view.frame.size.width - 55, 8)];[[progressView layer]setBorderColor:[UIColor whiteColor].CGColor]; 

      progressView.trackTintColor = [UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1.0]; 

      [progressView setProgress: ([customerArticleDetails.Logged_Hours floatValue]/[customerArticleDetails.Planned_Hours floatValue]) animated:YES]; 
      [cell.contentView addSubview:progressView]; 
... 
... 
return cell; 
    } 
+1

如果您正在使用故事板,請將progressview添加到SWTableViewCell並製作它的出口。然後,您不必在代碼中始終分配init UIProgressView。 progressview圖層上的初始化和更新使其滾動緩慢。 – Akhilrajtr

回答

0

它總是禁止和錯誤的每次需要呈現的時間添加UIViews細胞。

通過這種方式,您可以刪除可重複使用的單元格的目的,並且滾動非常糟糕。 每次瀏覽所有子視圖並刪除所有子視圖的成本更高。
它應該完成的方式是您創建一次進度視圖,然後根據需要刷新其進度。

這以兩種方式完成。

  1. 如果你有自己的服裝單元:

創建細胞UIProgress視圖並調用設置進度:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    SWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.backgroundColor = [UIColor clearColor]; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     if ([cell respondsToSelector:@selector(layoutMargins)]) { 
      cell.layoutMargins = UIEdgeInsetsZero; 
      cell.preservesSuperviewLayoutMargins = false; 
     } 
    } 

    // Animated YES is maybe a fine idea to be more nice, but it could be better if you use NO. 
    [cell.progressView setProgress: ([customerArticleDetails.Logged_Hours floatValue]/[customerArticleDetails.Planned_Hours floatValue]) animated:YES]; 
} 
  • 如果你不不擁有這個單元格,你需要用這種方法創建視圖。
  • 事情是這樣的:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        static NSString *CellIdentifier = @"Cell"; 
    
        SWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    
        if (cell == nil) 
        { 
         cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
         cell.backgroundColor = [UIColor clearColor]; 
    
         cell.selectionStyle = UITableViewCellSelectionStyleNone; 
         cell.accessoryType = UITableViewCellAccessoryNone; 
         if ([cell respondsToSelector:@selector(layoutMargins)]) { 
          cell.layoutMargins = UIEdgeInsetsZero; 
          cell.preservesSuperviewLayoutMargins = false; 
         } 
    
         UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; 
         [progressView setProgressTintColor:[UIColor colorWithRed:31.0/255 green:177.0/255 blue:137.0/255 alpha:1.0]]; 
         [[progressView layer]setCornerRadius:5.0f]; 
         [[progressView layer]setBorderWidth:2.0f]; 
         [[progressView layer]setMasksToBounds:TRUE]; 
         progressView.clipsToBounds = YES; 
         [[progressView layer]setFrame:CGRectMake(50, 93, self.view.frame.size.width - 55, 8)];[[progressView layer]setBorderColor:[UIColor whiteColor].CGColor]; 
    
         progressView.trackTintColor = [UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1.0]; 
         [progressView setTeg:10]; 
         [cell.contentView addSubview:progressView]; 
        } 
    
    
        UIProgressView *progressView = [cell.contentView viewWithTag:0] 
    
    
        // Animated YES is maybe a fine idea to be more nice, but it could be better if you use NO. 
        [progressView setProgress: ([customerArticleDetails.Logged_Hours floatValue]/[customerArticleDetails.Planned_Hours floatValue]) animated:YES]; 
    } 
    

    這樣你創建UIProgressview一次(所以沒有滾動的問題),並分別對每個小區只設置進度。

    相關問題