与图像的UITableview滚动非常缓慢

我有一个UITableView从服务器下载它的tableviewcells图像。

我观察到桌子滚动很慢。

我以为这可能是由于下载,但我已经意识到,下载完成后,表仍然滚动滚动,图像图标的大小是非常less的。

代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { btnBack.hidden = FALSE; static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; cell.selectionStyle = UITableViewCellSelectionStyleNone; ///////////////////// Cell other accessories ///////////////////// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.backgroundColor = [UIColor clearColor]; // cell.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"list_1.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]]; // cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"list_2.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]]; ///////////////////// Cell Title ///////////////////// cell.textLabel.font = [UIFont fontWithName:@"Noteworthy" size:17.0]; cell.textLabel.font = [UIFont boldSystemFontOfSize:17.0]; cell.textLabel.textColor = [UIColor blackColor]; cell.textLabel.highlightedTextColor = [UIColor blackColor]; } ///////////////////// Cell Title ///////////////////// cell.textLabel.text = [NSString stringWithFormat:@" %@", [test.arrTitle objectAtIndex:indexPath.row]]; ///////////////////// Cell Image ///////////////////// NSString *Path; Path = [NSString stringWithFormat:@"http://%@",[test.arrImages objectAtIndex:indexPath.row]]; NSLog(@"image-->%@",[test.arrImages objectAtIndex:indexPath.row]); NSString *strImage = Path; NSURL *url4Image = [NSURL URLWithString:strImage]; NSData *data = [NSData dataWithContentsOfURL:url4Image]; image =[[UIImage alloc] initWithData:data]; cell.imageView.image =image; [image release]; return cell; } 

你应该看看使用NSOperationQueue来处理延迟加载的图像和自定义tableviewcell。
谷歌的tweetie自定义tableviewcell这应该设置你在正确的方向。

苹果有一个示例项目,用于下载tableViews中的图像: LazyTableImages

也许你的list_bg_01.png图像很大? 这个方法占用大量的内存,只有在patternimage真的很小很简单时才应该使用它,在其他情况下最好使用UIImageView和image作为背景视图。

你有没有运行你的代码分析器? 正如Nik提到的那样,你应该使用GCD或者NSOperationasynchronous加载networking上的图像。 你可以看哈佛的iTunesUvideoNSOperation:它会给你一个清晰的例子,有很好的解释。

而且你绝对应该运行分析器,看看你花的时间最多。 具有less量图像的单元格应该加载得很快。

请注意,在初始化时(例如在单元格上设置背景颜色)可能会对单元格执行一些操作,如果反复执行操作,最终可能会耗费大量的性能。

Als如果你有一个特殊的布局,你应该inheritanceUITableViewCell并实现layoutSubviews方法(不要忘记调用super)。 在tableViewDataSourceDelegate中执行大量的工作(并强制执行重新布局)确实效率不高。

但是,再次运行探查器

我有同样的问题,并通过不使用cornerRadius属性更正。 然后我们用单元格的forms使用背景图像。

我想知道为什么你在视单元代码中创build你的图像。 难道你不能为整个class级创build一次,然后根据需要将其放到UIView中吗?

所有通用外观的configuration都应该在if(cell == nil){}只设置唯一的东西,比如标题等应该在外面。 另外,如果你的单元格的背景很简单(比如渐变),可以使用QuartzCore绘制它,或者使用CAGradientLayer作为子层到contentView的图层。