如何优化cellForRowAtIndexPath:代码

时间:2013-05-30 10:24:10

标签: ios objective-c uitableview

在为某些UILabels添加字体和阴影后,我注意到当视图从堆栈中弹出时,表视图动画滞后(像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;
}

感谢您的帮助。

6 个答案:

答案 0 :(得分: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 :(得分:1)

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

答案 2 :(得分:1)

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

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

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

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

答案 3 :(得分:1)

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

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

enter image description here

答案 4 :(得分: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;
}

答案 5 :(得分: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;

}