首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
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

Stack Overflow用户

发布于 2013-05-30 18:31:51

使用以下代码替换您的代码:

代码语言: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"];
        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
EN
查看全部 6 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16833688

复制
相关文章

相似问题

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