2013-05-10 84 views
0

当我有在我的tableview细胞滚动速度问题UITableView的滚动速度缓慢的问题。你可以看到它在出列和重用单元时挂起。下面是我用来创建单元格的代码。我使用2个自定义单元格,一个单元格是如果有图像,另一个单元格是用户没有附加图像。任何有识之士将不胜感激。我还应该补充一点,我想在单元格之间添加一个空格,所以基本上我创建了一个包含单个单元格的新部分,这就是为什么您总能在我的代码中看到indexPath.section。装载电池

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *imageCell = @"ShameImage"; 
     static NSString *noImageCell = @"ShameNoImageCell"; 
     static NSString *noCell = @"NoCell"; 

     UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:noCell]; 

     if ([[blobID objectAtIndex:indexPath.section] isEqualToNumber:[NSNumber numberWithInt:1]]) 
     { 
      ShameViewCell *cell = (ShameViewCell *)[self.tableView dequeueReusableCellWithIdentifier:imageCell]; 
      if (cell == nil) 
      { 
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ShameImageCell" owner:self options:nil]; 
       cell = [nib objectAtIndex:0]; 
       CALayer *cellImageView = [cell.imageView layer]; 
       [cellImageView setMasksToBounds:YES]; 
       [cellImageView setCornerRadius:10.0]; 
       IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)]; 
       [iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]]; 
       iiv.tag = 999; 
       [iiv loadImageFromURL]; 
       [cell.imageView addSubview:iiv]; 
       cell.imageView.userInteractionEnabled = YES; 
       UITapGestureRecognizer *tapImage = [[UITapGestureRecognizer alloc] 
                initWithTarget:self action:@selector(openPicture:)]; 
       tapImage.numberOfTapsRequired = 1; 
       [cell.imageView addGestureRecognizer:tapImage]; 

       [cell.contentView.layer setCornerRadius:10]; 
       [cell.contentView setBackgroundColor:[UIColor blackColor]]; 
       [cell.contentView setAlpha:0.7f]; 
       UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1]; 

       cell.lastShame.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0); 
       [cell.lastShame setTextColor:insideColor]; 
       [cell.lastShame setFont:[UIFont fontWithName:text_font_name size:14]]; 
       cell.lastShame.text = [userShame objectAtIndex:indexPath.section]; 

       [cell.shameDate setTextColor:insideColor]; 
       [cell.shameDate setFont:[UIFont fontWithName:text_font_name size:11]]; 
       cell.shameDate.text = [createDt objectAtIndex:indexPath.section]; 
       [cell.userLabel setTextColor:insideColor]; 
       [cell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]]; 
       cell.userLabel.text = [@"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]]; 
       [cell.lastShame setBackgroundColor:[UIColor clearColor]]; 

       return cell; 
      } 

     }else 
     { 
      ShameNoImage *cell = (ShameNoImage *)[self.tableView dequeueReusableCellWithIdentifier:noImageCell]; 
      if (cell == nil) 
      { 
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ShameNoImageCell" owner:self options:nil]; 
       cell = [nib objectAtIndex:0]; 
       CALayer *cellImageView = [cell.imageView layer]; 
       [cellImageView setMasksToBounds:YES]; 
       [cellImageView setCornerRadius:10.0]; 
       [cellImageView setBorderWidth:1.0]; 
       [cellImageView setBorderColor:[[UIColor whiteColor] CGColor]]; 
       [cell.contentView.layer setCornerRadius:10]; 
       [cell.contentView setBackgroundColor:[UIColor blackColor]]; 
       [cell.contentView setAlpha:0.7f]; 
       UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1]; 

       cell.shameLabel.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0); 
       [cell.shameLabel setTextColor:insideColor]; 
       [cell.shameLabel setFont:[UIFont fontWithName:text_font_name size:14]]; 
       cell.shameLabel.text = [userShame objectAtIndex:indexPath.section]; 

       [cell.dateLabel setTextColor:insideColor]; 
       [cell.dateLabel setFont:[UIFont fontWithName:text_font_name size:11]]; 
       cell.dateLabel.text = [createDt objectAtIndex:indexPath.section]; 
       [cell.userLabel setTextColor:insideColor]; 
       [cell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]]; 
       cell.userLabel.text = [@"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]]; 
       [cell.shameLabel setBackgroundColor:[UIColor clearColor]]; 
       return cell; 
      } 
     } 

     return cell; 
    } 
+0

如果'loadImageFromURL:'调用同步触击网络,那么您有滚动缓慢的主要原因之一。见http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html – 2013-05-10 22:36:20

+0

的IndicatorImageView不加载它们异步和下载过程中把一个活动指示灯在细胞中。另外我应该提到它缓存图像,并且在撤回缓存图像时仍然缓慢滚动。 – Jarod 2013-05-10 22:47:18

+0

使用仪器的时间探查,看看是什么导致的最大滞后 - 它可以让你深入到实际代码行是造成滞后/ – 2013-05-10 23:10:02

回答

0

我想通了。我正在重新创建和绘制每个单元格。我需要在Cell的子类中设置reuseIdentifier,然后将表的构建更改为这样。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *imageCell = @"ShameImage"; 
     static NSString *noImageCell = @"ShameNoImageCell"; 

     ShameViewCell *iCell = (ShameViewCell *)[self.tableView dequeueReusableCellWithIdentifier:imageCell]; 
     ShameNoImage *niCell = (ShameNoImage *)[self.tableView dequeueReusableCellWithIdentifier:noImageCell]; 

     if ([[blobID objectAtIndex:indexPath.section] isEqualToNumber:[NSNumber numberWithInt:1]]) 
     { 
      if (iCell == nil) 
      { 
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ShameImageCell" owner:self options:nil]; 
       iCell = [nib objectAtIndex:0]; 
       CALayer *cellImageView = [iCell.imageView layer]; 
       [cellImageView setMasksToBounds:YES]; 
       IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)]; 
       [iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]]; 
       iiv.tag = 999; 
       [iiv loadImageFromURL]; 
       [iCell.imageView addSubview:iiv]; 
       iCell.imageView.userInteractionEnabled = YES; 
       UITapGestureRecognizer *tapImage = [[UITapGestureRecognizer alloc] 
                initWithTarget:self action:@selector(openPicture:)]; 
       tapImage.numberOfTapsRequired = 1; 
       [iCell.imageView addGestureRecognizer:tapImage]; 

       [iCell.contentView setBackgroundColor:[UIColor blackColor]]; 
       [iCell.contentView setAlpha:0.7f]; 
       UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1]; 

       iCell.lastShame.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0); 
       [iCell.lastShame setTextColor:insideColor]; 
       [iCell.lastShame setFont:[UIFont fontWithName:text_font_name size:14]]; 
       iCell.lastShame.text = [userShame objectAtIndex:indexPath.section]; 

       [iCell.shameDate setTextColor:insideColor]; 
       [iCell.shameDate setFont:[UIFont fontWithName:text_font_name size:11]]; 
       iCell.shameDate.text = [createDt objectAtIndex:indexPath.section]; 
       [iCell.userLabel setTextColor:insideColor]; 
       [iCell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]]; 
       iCell.userLabel.text = [@"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]]; 

       return iCell; 
      }else 
      { 
       [[iCell.imageView viewWithTag:999] removeFromSuperview]; 
       IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)]; 
       [iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]]; 
       iiv.tag = 999; 
       [iiv loadImageFromURL]; 
       [iCell.imageView addSubview:iiv]; 
       iCell.lastShame.text = [userShame objectAtIndex:indexPath.section]; 
       iCell.shameDate.text = [createDt objectAtIndex:indexPath.section]; 
       iCell.userLabel.text = [@"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]]; 

       return iCell; 
      } 

     }else 
     { 
      if (niCell == nil) 
      { 
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ShameNoImageCell" owner:self options:nil]; 
       niCell = [nib objectAtIndex:0]; 
       [niCell.contentView setBackgroundColor:[UIColor blackColor]]; 
       [niCell.contentView setAlpha:0.7f]; 
       UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1]; 

       niCell.shameLabel.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0); 
       [niCell.shameLabel setTextColor:insideColor]; 
       [niCell.shameLabel setFont:[UIFont fontWithName:text_font_name size:14]]; 
       niCell.shameLabel.text = [userShame objectAtIndex:indexPath.section]; 

       [niCell.dateLabel setTextColor:insideColor]; 
       [niCell.dateLabel setFont:[UIFont fontWithName:text_font_name size:11]]; 
       niCell.dateLabel.text = [createDt objectAtIndex:indexPath.section]; 
       [niCell.userLabel setTextColor:insideColor]; 
       [niCell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]]; 
       niCell.userLabel.text = [@"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]]; 
       return niCell; 
      }else 
      { 
       niCell.shameLabel.text = [userShame objectAtIndex:indexPath.section]; 
       niCell.dateLabel.text = [createDt objectAtIndex:indexPath.section]; 
       niCell.userLabel.text = [@"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]]; 
       return niCell; 
      } 
     } 
     return niCell; 
    } 
0

跳出来的第一件事是拨打setCornerRadius。一个快速测试将切出角落半径的东西,看看是否有助于你的速度。如果这是你的问题,你将不得不切换到使用CG和UIBezierPath进行绘图。

看看接下来的事情将是透明和你正在使用的子视图的数量。很难说上面发布的内容是什么,但是更多的子视图,尤其是alpha,这意味着更多的工作合成。但是,根据您的设计,这可能很难实现,但使用CG进行绘图或使用图像可以提供帮助。

+0

你有没有关于如何使用UIBezierPath的例子? – Jarod 2013-05-11 01:43:57

+0

这里是[萨姆Soffes切达应用](https://github.com/nothingmagical/cheddar-ios/blob/master/Classes/CDIGroupedTableViewCell.m)的一个例子。这是你问题的根源吗? – Anthony 2013-05-14 16:58:23