由于阴影和边框而导致UITableView滞后

我有以下代码来添加边框颜色和阴影到我的UITableViewCell的背景。 我的问题是这段代码会导致tableView本身出现大量延迟。

请问您能告诉我如何优化代码,防止UITableView的延迟?

if ([cell viewWithTag:012] == nil && comment.isReply == NO) { UIImageView *iv = [[[UIImageView alloc] initWithFrame:frame] autorelease]; [iv setImage:[UIImage imageNamed:@"paper"]]; [iv setTag:012]; [cell insertSubview:iv atIndex:0]; [iv.layer setBorderWidth:1.0]; [iv.layer setBorderColor:[[UIColor whiteColor] CGColor]]; [iv.layer setShadowColor:[[UIColor blackColor] CGColor]]; [iv.layer setShadowOffset:CGSizeMake(0, 1)]; [iv.layer setShadowOpacity:0.75]; } else if ([cell viewWithTag:012] == nil && comment.isReply == YES) { frame.origin.x += 35; UIImageView *iv = [[[UIImageView alloc] initWithFrame:frame] autorelease]; [iv setImage:[UIImage imageNamed:@"paper"]]; [iv setTag:012]; [cell insertSubview:iv atIndex:0]; UIImage *arrow = [UIImage imageNamed:@"arrow"]; UIImageView *ivs = [[[UIImageView alloc] initWithFrame:CGRectMake(-12, ([cell frame].size.width / 2) + ([arrow size].width/2) , arrow.size.width, arrow.size.height)] autorelease]; [cell addSubview:ivs]; [iv.layer setBorderWidth:1.0]; [iv.layer setBorderColor:[[UIColor whiteColor] CGColor]]; [iv.layer setShadowColor:[[UIColor blackColor] CGColor]]; [iv.layer setShadowOffset:CGSizeMake(0, 0)]; [iv.layer setShadowOpacity:0.75]; } 

您应该避免在每次加载时操纵单元格,而应该在初始化/创建单元格时调整UI。

为了说明,每次滚动时都可以使用cellForRowAtIndexPath:方法加载新单元格(或多个),目前您在此方法中进行了大量的视图更改,但可能会出现不需要这种情况的情况(例如新单元格与刚刚滚出屏幕的单元格类型相同。 将此UI修改移动到初始化单元格的位置,而不是交换数据的位置。 你可以用子类或者像这样做。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Reuse id static NSString *identifier1 = @"identifer-1"; static NSString *identifier2 = @"identifier-2"; static NSString *regular = @"regular"; UITableViewCell *cell; if (comment.isReply == NO) { cell = [tableView dequeueReusableCellWithIdentifier: identifier1]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identifier1] autorelease]; // Do the UI modification here } } else if (comment.isReply == YES) { cell = [tableView dequeueReusableCellWithIdentifier: identifier2]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identifier2] autorelease]; // Do the UI modification here } } else { // Regular cell cell = [tableView dequeueReusableCellWithIdentifier: regular]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: regular] autorelease]; } } // Load the data into the cell return cell; } 

希望你能得到我的目标,关键是尽可能少地做重物并让UITableView缓存产生更大的效果。

除了此处的其他优化建议shadowPath ,在CALayer上指定shadowPath将提高阴影绘制性能。 你可以用这样的东西确定阴影的路径……

 iv.layer.shadowPath = [UIBezierPath bezierPathWithRect:iv.bounds].CGPath; 

您可能还想查看CALayer上的shouldRasterize位。 这会导致图层预渲染为位图。 如果你走这条路线,一定要提供与你的设备匹配的rasterizationScale。

 cell.layer.shouldRasterize = YES; cell.layer.rasterizationScale = [UIScreen mainScreen].scale;