2013-05-10 91 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