Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何优化cellForRowAtIndexPath:代码_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 如何优化cellForRowAtIndexPath:代码

Ios 如何优化cellForRowAtIndexPath:代码,ios,objective-c,uitableview,Ios,Objective C,Uitableview,在给一些UILabel添加字体和阴影后,我注意到当视图从堆栈中弹出时,表视图动画会滞后(像FB/Path使用的侧击)。在我添加UILabel阴影之前,侧扫是平滑的 我想我可能是把它添加到了错误的位置,所以标签属性可能被错误地添加了。请查看以下cellforrowatinexpath:方法: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

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

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

- (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;
}

感谢您的帮助。

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

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

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

- (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;
}    

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

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

添加到您的
表格视图:cellforrowatinexpath:
方法:

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

将代码替换为以下内容:

- (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;
}

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

(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 静态NSString*cellReuseIdentifier=@“cellReuseIdentifier”; UITableCustomViewCell*单元格=[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;

}这篇来自Twitter工程的文章为您提供了一个很好的概述:

基本上,您希望避免使用子视图,而是直接使用Quartz绘制内容。这是提高性能所能做的最好的事情。另外:避免透明度!在工具中,还可以使用核心动画工具并激活“颜色混合层”,以查看组成透明视图的位置:


标题标签.layer.shouldRasterize=是;detailLabel.layer.shouldRasterize=YES@阿马尔:谢谢!这就不同了,但也要检查@Wain!!凉的但是你应该按照@Wainyep的建议优化你的单元格创建代码。我也改变了。再次感谢