0

我的 tableview Cells 滚动速度有问题。您可以看到它在出列和重用单元格时挂起。下面是我用来创建单元格的代码。我正在使用 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;
    }
4

2 回答 2

0

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

    - (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;
    }
于 2013-05-11T03:06:00.787 回答
0

首先跳出来的是对setCornerRadius. 一个快速的测试是剪掉那个角落半径的东西,看看这是否有助于你的速度。如果那是您的问题,您将不得不切换到使用 CG 和UIBezierPath.

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

于 2013-05-10T23:09:55.480 回答