将 UIView 添加到 cell.contentView 时的 UITableView 性能问题

2024-02-20

我在使用某些子视图时遇到性能问题UITableViewCells。当我继续滚动后,它最终开始变得非常慢。

我要做的第一步是创建一个共同的UIView对于每个单元格,本质上这是创建一个白色单元格,在带有阴影的单元格上具有圆形效果。这个表现似乎很正常,所以我不认为这是罪魁祸首。

这是我用来执行此操作的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *NewsCellIdentifer = @"NewsCellIdentifier";


       NewsItem *item = [self.newsArray objectAtIndex:indexPath.row];


            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NewsCellIdentifer];

            if (cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NewsCellIdentifer];


                cell.contentView.backgroundColor = [UIColor clearColor];

                UIView *whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(10,10,300,100)];
                whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
                whiteRoundedCornerView.layer.masksToBounds = NO;
                whiteRoundedCornerView.layer.cornerRadius = 3.0;
                whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-1, 1);
                whiteRoundedCornerView.layer.shadowOpacity = 0.5;

                [cell.contentView addSubview:whiteRoundedCornerView];
                [cell.contentView sendSubviewToBack:whiteRoundedCornerView];

                cell.layer.shouldRasterize = YES;
                cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
                cell.layer.opaque = YES;

                cell.opaque = YES;    
            }

            [cell.contentView addSubview:[self NewsItemThumbnailView:item]];

            return cell;
}

以下是返回图形和文本的缩略图视图的方法:

- (UIView *) NewsItemThumbnailView:(NewsItem *)item
{
    UIView *thumbNailMainView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 50, 70)];
    UIImageView *thumbNail = [[UIImageView alloc] initWithImage:[UIImage imageNamed:item.ThumbNailFileName]];
    thumbNail.frame = CGRectMake(10,10, 45, 45);
    UILabel *date = [[UILabel alloc] init];
    date.frame = CGRectMake(10, 53, 45, 12);
    date.text = item.ShortDateString;
    date.textAlignment = NSTextAlignmentCenter;
    date.textColor = [BVColors WebDarkGrey];
    CGFloat fontSize = 10.0;
    date.font = [BVFont Museo:&fontSize];

    date.opaque = YES;
    thumbNail.opaque = YES;
    thumbNailMainView.opaque = YES;


    [thumbNailMainView addSubview:thumbNail];
    [thumbNailMainView addSubview:date];

    return thumbNailMainView;
}

性能问题似乎是当我将缩略图视图添加到单元格时,因为当我注释该行时,我似乎没有它。缩略图信息是动态的,并且会随着每个单元格的变化而变化。对于如何在不降低性能的情况下执行此操作的任何建议,我将不胜感激。


UITableView将会通知tableView:cellForRowAtIndexPath:每次看到一个单元格时,并且dequeueReusableCellWithIdentifier:将重用现有的单元格对象(如果可用)。这两个事实结合在一起,使您陷入这样的场景:每次滚动时,相同的有限数量的单元格对象最终会产生越来越多的子视图。

正确的方法是创建一个自定义的UITableViewCell具有属性的子类thumbnailView。在该属性的设置器中,删除上一个缩略图(如果有),然后将新缩略图添加到contentView。这可确保您在任何时候都只有一个缩略图子视图。

一个不太理想的方法是将标签添加到UIView从返回NewsItemThumbnailView (thumbNailMainView.tag = someIntegerConstant),然后搜索带有该标签的任何视图并在添加另一个视图之前将其删除:

 // remove old view
 UIView *oldThumbnailView = [cell.contentView viewWithTag:someIntegerConstant];
 [oldThumbnailView removeFromSuperview];

 // add new view
 [cell.contentView addSubview:[self NewsItemThumbnailView:item]];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 UIView 添加到 cell.contentView 时的 UITableView 性能问题 的相关文章

随机推荐