2013-05-30 50 views
0

向某些UILabels添加字体和阴影后,我注意到当视图从堆栈弹出(像FB/Path使用的侧滑动)时,表视图动画滞后。在添加UILabel阴影之前,侧滑动是平滑的。如何优化cellForRowAtIndexPath:代码

我想我可能会添加它在错误的地方,以便标签属性被错误地添加也许。请看看以下cellForRowAtIndexPath:以下方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * cellReuseIdentifier = @"cellReuseIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier]; 
    } 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)]; 
    imageView.image = [UIImage imageNamed:@"rest.jpg"]; 
    [cell.contentView addSubview:imageView]; 

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)]; 

    titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"]; 
    titleLabel.backgroundColor = [UIColor clearColor]; 

    titleLabel.textColor = [UIColor whiteColor]; 
    [titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:24]]; 
    titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
    titleLabel.layer.shadowOpacity = 0.7; 

    [cell.contentView addSubview:titleLabel]; 

    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)]; 

    detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"description"]; 
    detailLabel.backgroundColor = [UIColor clearColor]; 

    detailLabel.textColor = [UIColor whiteColor]; 
    [detailLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18]]; 
    detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
    detailLabel.layer.shadowOpacity = 0.7; 

    [cell.contentView addSubview:detailLabel]; 

    cell.contentView.backgroundColor = [UIColor clearColor]; 


    return cell; 
} 

感谢您的帮助。

+2

titleLabel.layer.shouldRasterize = YES; detailLabel.layer.shouldRasterize = YES; – Amar

+0

@Amar谢谢!那做出了区别 – hanumanDev

+0

但也检查@Wain的答案! –

回答

4

你总是添加新的子视图。所以,无论何时滚动表格视图,您的单元格都会添加越来越多的内容。

创建单元格时创建所有子视图,然后更新子视图设置。例如:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * cellReuseIdentifier = @"cellReuseIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier]; 

     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)]; 
     imageView.tag = 123123; 
     [cell.contentView addSubview:imageView]; 

     UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)]; 

     titleLabel.tag = 234234]; 
     titleLabel.backgroundColor = [UIColor clearColor]; 

     titleLabel.textColor = [UIColor whiteColor]; 
     [titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:24]]; 
     titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
     titleLabel.layer.shadowOpacity = 0.7; 

     [cell.contentView addSubview:titleLabel]; 

     UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)]; 

     detailLabel.tag = 345345]; 
     detailLabel.backgroundColor = [UIColor clearColor]; 

     detailLabel.textColor = [UIColor whiteColor]; 
     [detailLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18]]; 
     detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
     detailLabel.layer.shadowOpacity = 0.7; 

     [cell.contentView addSubview:detailLabel]; 

     cell.contentView.backgroundColor = [UIColor clearColor]; 
    } 

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:123123]; 
    imageView.image = [UIImage imageNamed:@"rest.jpg"]; 

    UILabel *titleLabel = (UILabel *)[cell viewWithTag:234234]; 
    titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"]; 

    UILabel *detailLabel = (UILabel *)[cell viewWithTag:345345]; 
    detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"description"]; 

    return cell; 
}  
1

由于文本属性永远不会改变,请移动在if语句中设置它们的代码。只保留将图像和标签文字设置在if声明之外的代码。单元格被重用,所以像字体等属性将保留在单元中,即使它被“回收”了。在else分支中添加用于查找单元格中现有标签的代码。否则,您会不断向细胞添加相同的标签多次。

1

即使在出列之后,您也添加子视图,而不是初始化新的单元格。确保所有创建和添加子视图的代码仅在初始化单元格时完成。如果您需要引用单元格中的视图进行配置,请使用子类UITableViewCell。

此外,阴影渲染可以减缓下来过,加shadowpath使渲染更加高效:

添加到您的tableView:cellForRowAtIndexPath:方法:

... 
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:detailLabel.layer.bounds].CGPath; 
detailLabel.layer.shadowPath = shadowPath; 
... 
0

这种替换代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * cellReuseIdentifier = @"cellReuseIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier]; 


     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)]; 
     imageView.image = [UIImage imageNamed:@"rest.jpg"]; 
     imageView.tag =1; 
     [cell.contentView addSubview:imageView]; 

     UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)]; 

     titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"]; 
     titleLabel.backgroundColor = [UIColor clearColor]; 
     titleLabel.tag = 2; 
     titleLabel.textColor = [UIColor whiteColor]; 
     [titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:24]]; 
     titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
     titleLabel.layer.shadowOpacity = 0.7; 

     [cell.contentView addSubview:titleLabel]; 

     UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)]; 
     detailLabel.tag = 3; 
     detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"description"]; 
     detailLabel.backgroundColor = [UIColor clearColor]; 

     detailLabel.textColor = [UIColor whiteColor]; 
     [detailLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18]]; 
     detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
     detailLabel.layer.shadowOpacity = 0.7; 


    } 

    UIImageView *tempImgView = (UIImageView *)[cell viewWithTag:1]; 
    tempImgView.image = [UIImage imageNamed:@""];// Here you can set any image by reusing imageview without allocating again and again 

    UILabel *tempLabel; 

    tempLabel = (UILabel *)[cell viewWithTag:2]; 
    tempLabel.text = @"";// Here you can access your title label and can set its properties without allocating again 

    tempLabel = (UILabel *)[cell viewWithTag:3]; 
    tempLabel.text = @"";// Here you can access your detailLabel label and can set its properties without allocating again 


    [cell.contentView addSubview:detailLabel]; 

    cell.contentView.backgroundColor = [UIColor clearColor]; 


    return cell; 
} 
0

您需要创建具有类的自定义表格视图单元格。此代码添加了许多标签,然后阴影显示没有正确。 这样的代码。

(的UITableViewCell *)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath { 静态的NSString * cellReuseIdentifier = @ “cellReuseIdentifier”; UITableCustomViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];

if (cell == nil) 
{ 
    cell = [[UITableCustomViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier]; 
} 

cell.imageView.image = [UIImage imageNamed:@"rest.jpg"]; 

cell.titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"]; 

cell.detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"description"]; 

return cell; 

}

1

这个来自Twitter的工程文章为您提供了一个很好的概述:http://engineering.twitter.com/2012/02/simple-strategies-for-smooth-animation.html

基本上,你要避免使用子视图,而是直接与石英画出您的内容。这是你可以做的改善性能的最好的事情。另外:避免透明!在仪器,也可以使用核心动画仪器并激活“颜色共混层”上看到透明视图正在组成:

enter image description here