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

时间:2013-05-10 22:09:46

标签: iphone ios objective-c uitableview scroll

我遇到了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;
    }

2 个答案:

答案 0 :(得分:0)

跳出来的第一件事是对setCornerRadius的调用。一个快速的测试是削减角半径的东西,看看是否有助于你的速度。如果您遇到问题,则必须使用CG和UIBezierPath切换到绘图。

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

答案 1 :(得分:0)

我明白了。我正在重建和绘制每一个细胞。我需要在Cell的Subclass中设置reuseIdentifier,然后将表的构建更改为这样。

    - (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;
    }
相关问题