繁体   English   中英

加载单元格时,UITableView滚动缓慢问题

[英]UITableView Scrolling slowness issues when loading cell

我的表格单元格滚动速度出现问题。 您可以看到,在使单元出队和重新使用时,它挂断了。 以下是我用于创建单元格的代码。 我正在使用2个自定义单元格,一个单元格是是否有图像,而另一个单元格是如果用户未附加图像。 任何见识将不胜感激。 我还应该补充一点,我想在单元格之间添加一个空间,因此基本上我创建了一个包含单个单元格的新节,这就是为什么您总是在我的代码indexPath.section中看到的原因。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *imageCell = @"ShameImage";
        static NSString *noImageCell = @"ShameNoImageCell";
        static NSString *noCell = @"NoCell";

        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:noCell];

        if ([[blobID objectAtIndex:indexPath.section] isEqualToNumber:[NSNumber numberWithInt:1]])
        {
            ShameViewCell *cell = (ShameViewCell *)[self.tableView dequeueReusableCellWithIdentifier:imageCell];
            if (cell == nil)
            {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ShameImageCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
                CALayer *cellImageView = [cell.imageView layer];
                [cellImageView setMasksToBounds:YES];
                [cellImageView setCornerRadius:10.0];
                IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
                [iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
                iiv.tag = 999;
                [iiv loadImageFromURL];
                [cell.imageView addSubview:iiv];
                cell.imageView.userInteractionEnabled = YES;
                UITapGestureRecognizer *tapImage = [[UITapGestureRecognizer alloc]
                                                    initWithTarget:self action:@selector(openPicture:)];
                tapImage.numberOfTapsRequired = 1;
                [cell.imageView addGestureRecognizer:tapImage];

                [cell.contentView.layer setCornerRadius:10];
                [cell.contentView setBackgroundColor:[UIColor blackColor]];
                [cell.contentView setAlpha:0.7f];
                UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];

                cell.lastShame.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
                [cell.lastShame setTextColor:insideColor];
                [cell.lastShame setFont:[UIFont fontWithName:text_font_name size:14]];
                cell.lastShame.text = [userShame objectAtIndex:indexPath.section];

                [cell.shameDate setTextColor:insideColor];
                [cell.shameDate setFont:[UIFont fontWithName:text_font_name size:11]];
                cell.shameDate.text = [createDt objectAtIndex:indexPath.section];
                [cell.userLabel setTextColor:insideColor];
                [cell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
                cell.userLabel.text = [@"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
                [cell.lastShame setBackgroundColor:[UIColor clearColor]];

                return cell;
            }

        }else
        {
            ShameNoImage *cell = (ShameNoImage *)[self.tableView dequeueReusableCellWithIdentifier:noImageCell];
            if (cell == nil)
            {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ShameNoImageCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
                CALayer *cellImageView = [cell.imageView layer];
                [cellImageView setMasksToBounds:YES];
                [cellImageView setCornerRadius:10.0];
                [cellImageView setBorderWidth:1.0];
                [cellImageView setBorderColor:[[UIColor whiteColor] CGColor]];
                [cell.contentView.layer setCornerRadius:10];
                [cell.contentView setBackgroundColor:[UIColor blackColor]];
                [cell.contentView setAlpha:0.7f];
                UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];

                cell.shameLabel.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
                [cell.shameLabel setTextColor:insideColor];
                [cell.shameLabel setFont:[UIFont fontWithName:text_font_name size:14]];
                cell.shameLabel.text = [userShame objectAtIndex:indexPath.section];

                [cell.dateLabel setTextColor:insideColor];
                [cell.dateLabel setFont:[UIFont fontWithName:text_font_name size:11]];
                cell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
                [cell.userLabel setTextColor:insideColor];
                [cell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
                cell.userLabel.text = [@"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
                [cell.shameLabel setBackgroundColor:[UIColor clearColor]];
                return cell;
            }
        }

        return cell;
    }

跳出的第一件事是对setCornerRadius的调用。 一个快速的测试是将那些圆角半径的东西切掉,看看是否对您的速度有所帮助。 如果这是您的问题,则必须切换到使用CG和UIBezierPath进行绘制。

接下来要看的是透明度和正在使用的子视图的数量。 上面的内容很难说明,但是更多的子视图,尤其是alpha子视图将意味着更多的工作合成。 但这可能很难解决,具体取决于您的设计,但是使用CG绘图或使用图像可以有所帮助。

我想到了。 我正在重新创建并绘制每个单元。 我需要在Cell的子类中设置复用标识符,然后将表的构建更改为这样。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *imageCell = @"ShameImage";
        static NSString *noImageCell = @"ShameNoImageCell";

        ShameViewCell *iCell = (ShameViewCell *)[self.tableView dequeueReusableCellWithIdentifier:imageCell];
        ShameNoImage *niCell = (ShameNoImage *)[self.tableView dequeueReusableCellWithIdentifier:noImageCell];

        if ([[blobID objectAtIndex:indexPath.section] isEqualToNumber:[NSNumber numberWithInt:1]])
        {
            if (iCell == nil)
            {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ShameImageCell" owner:self options:nil];
                iCell = [nib objectAtIndex:0];
                CALayer *cellImageView = [iCell.imageView layer];
                [cellImageView setMasksToBounds:YES];
                IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
                [iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
                iiv.tag = 999;
                [iiv loadImageFromURL];
                [iCell.imageView addSubview:iiv];
                iCell.imageView.userInteractionEnabled = YES;
                UITapGestureRecognizer *tapImage = [[UITapGestureRecognizer alloc]
                                                    initWithTarget:self action:@selector(openPicture:)];
                tapImage.numberOfTapsRequired = 1;
                [iCell.imageView addGestureRecognizer:tapImage];

                [iCell.contentView setBackgroundColor:[UIColor blackColor]];
                [iCell.contentView setAlpha:0.7f];
                UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];

                iCell.lastShame.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
                [iCell.lastShame setTextColor:insideColor];
                [iCell.lastShame setFont:[UIFont fontWithName:text_font_name size:14]];
                iCell.lastShame.text = [userShame objectAtIndex:indexPath.section];

                [iCell.shameDate setTextColor:insideColor];
                [iCell.shameDate setFont:[UIFont fontWithName:text_font_name size:11]];
                iCell.shameDate.text = [createDt objectAtIndex:indexPath.section];
                [iCell.userLabel setTextColor:insideColor];
                [iCell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
                iCell.userLabel.text = [@"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];

                return iCell;
            }else
            {
                [[iCell.imageView viewWithTag:999] removeFromSuperview];
                IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
                [iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
                iiv.tag = 999;
                [iiv loadImageFromURL];
                [iCell.imageView addSubview:iiv];
                iCell.lastShame.text = [userShame objectAtIndex:indexPath.section];
                iCell.shameDate.text = [createDt objectAtIndex:indexPath.section];
                iCell.userLabel.text = [@"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];

                return iCell;
            }

        }else
        {
            if (niCell == nil)
            {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ShameNoImageCell" owner:self options:nil];
                niCell = [nib objectAtIndex:0];
                [niCell.contentView setBackgroundColor:[UIColor blackColor]];
                [niCell.contentView setAlpha:0.7f];
                UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];

                niCell.shameLabel.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
                [niCell.shameLabel setTextColor:insideColor];
                [niCell.shameLabel setFont:[UIFont fontWithName:text_font_name size:14]];
                niCell.shameLabel.text = [userShame objectAtIndex:indexPath.section];

                [niCell.dateLabel setTextColor:insideColor];
                [niCell.dateLabel setFont:[UIFont fontWithName:text_font_name size:11]];
                niCell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
                [niCell.userLabel setTextColor:insideColor];
                [niCell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
                niCell.userLabel.text = [@"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
                return niCell;
            }else
            {
                niCell.shameLabel.text = [userShame objectAtIndex:indexPath.section];
                niCell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
                niCell.userLabel.text = [@"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
                return niCell;
            }
        }
        return niCell;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM