首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何优化cellForRowAtIndexPath:代码

如何优化cellForRowAtIndexPath:代码
EN

Stack Overflow用户
提问于 2013-05-30 18:24:10
回答 6查看 916关注 0票数 0

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

我想我可能把它添加到了错误的位置,所以标签属性可能被错误地添加了。请看下面的cellForRowAtIndexPath:方法:

代码语言:javascript
复制
- (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;
}

谢谢你的帮助。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-05-30 18:30:20

您总是在添加新的子视图。因此,每当您滚动表格视图时,单元格中添加的内容就会越来越多。

在创建单元格时创建所有子视图,然后只更新子视图设置。类似于:

代码语言:javascript
复制
- (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;
}    
票数 4
EN

Stack Overflow用户

发布于 2013-05-30 18:28:32

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

票数 1
EN

Stack Overflow用户

发布于 2013-05-30 18:30:53

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

此外,阴影渲染也可能会减慢它,添加一个阴影路径来使渲染更有效:

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

代码语言:javascript
复制
...
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:detailLabel.layer.bounds].CGPath;
detailLabel.layer.shadowPath = shadowPath;
...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16833688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档