2011-11-30 68 views
5

我有以下代码添加边框颜色和投影到我的UITableViewCell的背景。我的问题是,这段代码会导致tableView本身的巨大滞后。UITableView滞后由于阴影和边界

请你可以告诉我如何优化我的代码,防止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]; 

} 
+0

你究竟在哪里添加阴影?我的意思是你在UITableView数据源中这样做吗?如果是这样,你应该真的考虑创建一个自定义的UITableViewCell子类并在那里做自定义用户界面。 –

+0

对不起,我没有说清楚,那是在cellForRowAtIndexPath方法。 –

回答

1

您应该避免在每次加载时操作单元格,而应该在单元格初始化/创建时调整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的缓存具有更大的影响。

20

除了这里的其他优化建议,在您的CALayer上指定shadowPath将提高阴影绘制性能。你能确定路径与像这样的影子...

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

您可能还需要寻找到了shouldRasterize位上的CALayer。这导致图层被预渲染为位图。如果你走这条路线,一定要提供一个与你的设备匹配的rasterizationScale。

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

看看这个很棒的[博客文章](http://nachbaur.com/blog/fun-shadow-effects-using-custom-calayer-shadowpaths)和一些不同的shadowPath,你可以使用 –

+1

非常感谢这个关于光栅化的建议!为我做了诡计;)现在我的tableView滚动得如此顺利^^ – polo987

+0

OMG,谢谢!对不起,我只能投票给你一次。 –