在UITableViewCell中放置UIProgressView时出现问题

时间:2015-11-03 05:49:21

标签: ios uitableview uiprogressview

我在UIProgressView中放置了UITableViewCell。我面临两个问题:
1. UITableView卷轴受到阻碍 2.每次(显然)调用UIProgressView setProgress ,每次出现时都会设置进度。

问题1 问题2 而发生,这是可以理解的。为了测试这一点,我对UIProgressView中的UITableViewCell进行了评论。滚动速度非常快。
这是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 个答案:

答案 0 :(得分:0)

每次需要渲染时,在单元格中添加UIViews总是禁止和错误的。

通过这种方式,您可以删除可重用单元格的用途,并且滚动非常糟糕。 每次浏览所有子视图并删除所有子视图的成本甚高 应该这样做的方法是创建一次进度视图,然后根据需要刷新进度。

这有两种方式。

  1. 如果你有自己的服装细胞:
  2. 在单元格中创建UIProgress视图,只调用set progress:

    - (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]; 
    }
    
    1. 如果您不拥有该单元格,则需要在此方法中创建视图。
    2. 这样的事情:

      - (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(因此没有滚动问题),只需单独设置每个单元格的进度。

相关问题