當我有在我的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;
}
如果'loadImageFromURL:'調用同步觸擊網絡,那麼您有滾動緩慢的主要原因之一。見http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html – 2013-05-10 22:36:20
的IndicatorImageView不加載它們異步和下載過程中把一個活動指示燈在細胞中。另外我應該提到它緩存圖像,並且在撤回緩存圖像時仍然緩慢滾動。 – Jarod 2013-05-10 22:47:18
使用儀器的時間探查,看看是什麼導致的最大滯後 - 它可以讓你深入到實際代碼行是造成滯後/ – 2013-05-10 23:10:02