0

在向一些 UILabel 添加字体和阴影后,我注意到当视图从堆栈中弹出时表视图动画滞后(像 FB/Path 使用的侧面滑动)。在我添加 UILabel 阴影之前,侧面滑动是平滑的。

我想我可能将它添加到错误的位置,因此标签属性可能被错误地添加。请看下面的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;
}

谢谢你的帮助。

4

6 回答 6

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;
}    
于 2013-05-30T10:30:19.663 回答
1

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

于 2013-05-30T10:28:31.700 回答
1

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

此外,阴影渲染也可能会减慢速度,添加阴影路径以提高渲染效率:

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

...
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:detailLabel.layer.bounds].CGPath;
detailLabel.layer.shadowPath = shadowPath;
...
于 2013-05-30T10:30:53.123 回答
1

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

基本上,您希望避免使用子视图,而是直接使用 Quartz 绘制内容。这是你可以做的最好的事情来提高性能。另外:避免透明!在 Instruments 中,您还可以使用 Core Animation 工具并激活“Color Blended Layers”来查看透明视图的组合位置:

在此处输入图像描述

于 2013-05-30T11:38:05.880 回答
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;
}
于 2013-05-30T10:31:50.987 回答
0

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

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static 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;

}

于 2013-05-30T10:36:00.447 回答