使用UIImage和UILabels创buildUICollectionViewCell会创build慢速滚动

我创build了一个UIview(充当头),一个UIImage和10个UILabels的子视图。 我把这些放到UICollectionView中作为单元格。

完全devise时,不会平滑滚动。 如果我删除所有的UILabels,它顺利滚动。

我假设它是迟缓的导致UICollectionView按需加载,所以当它需要每个新的单元格,它必须绘制它locking主线程。

iOS只是为了处理它们而创build它们太多了吗? 如果是这样,还有另外一种方法可以把文本放进去吗?

我的细胞是什么样的:

在这里输入图像说明

这里是DatasetFilterListPanelView,它创build了我放入UICollectionViewCell的UIView。 我这样做,因为我决定使用UICollectionView之前,我创build了这个。

@implementation DatasetFilterListPanelView -(id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.translatesAutoresizingMaskIntoConstraints = FALSE; UIView *contentView = [self createContentView]; [self addSubview:contentView]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(contentView)]]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(contentView)]]; } return self; } -(UIView *) createContentView { UIView *contentView = [[UIView alloc] initWithFrame:self.frame]; // contentView.translatesAutoresizingMaskIntoConstraints = FALSE; contentView.backgroundColor = [UIColor myDarkGrayColor]; UIView *headerView = [self createHeaderView]; [contentView addSubview:headerView]; [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[headerView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(headerView)]]; [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[headerView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(headerView)]]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gear12.png"]]; imageView.translatesAutoresizingMaskIntoConstraints = FALSE; imageView.backgroundColor = [UIColor blueColor]; self.imageView = imageView; [imageView addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:imageView attribute:NSLayoutAttributeWidth multiplier:1 constant:0]]; [contentView addSubview:imageView]; [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[imageView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(imageView)]]; [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[headerView]-[imageView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(headerView, imageView)]]; UILabel *acresLabel = [self createLabelWithTitle:@"Label01:" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:12]]; [contentView addSubview:acresLabel]; UILabel *addedLabel = [self createLabelWithTitle:@"Label02:" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:12]]; [contentView addSubview:addedLabel]; UILabel *typeLabel = [self createLabelWithTitle:@"Label03:" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:12]]; [contentView addSubview:typeLabel]; UILabel *zonesLabel = [self createLabelWithTitle:@"Label04:" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:12]]; [contentView addSubview:zonesLabel]; UILabel *sceneLabel = [self createLabelWithTitle:@"Label05:" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:12]]; [contentView addSubview:sceneLabel]; UILabel *acresValueLabel = [self createLabelWithTitle:@"Data" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE size:12]]; acresValueLabel.textAlignment = NSTextAlignmentLeft; [contentView addSubview:acresValueLabel]; UILabel *addedValueLabel = [self createLabelWithTitle:@"Data" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE size:12]]; addedValueLabel.textAlignment = NSTextAlignmentLeft; [contentView addSubview:addedValueLabel]; UILabel *typeValueLabel = [self createLabelWithTitle:@"Name" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE size:12]]; typeValueLabel.textAlignment = NSTextAlignmentLeft; [contentView addSubview:typeValueLabel]; UILabel *zonesValueLabel = [self createLabelWithTitle:@"Data" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE size:12]]; zonesValueLabel.textAlignment = NSTextAlignmentLeft; [contentView addSubview:zonesValueLabel]; UILabel *sceneValueLabel = [self createLabelWithTitle:@"Name" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE size:12]]; sceneValueLabel.textAlignment = NSTextAlignmentLeft; [contentView addSubview:sceneValueLabel]; NSDictionary *views = NSDictionaryOfVariableBindings(headerView, imageView, acresLabel, acresValueLabel, addedLabel, addedValueLabel, typeLabel, typeValueLabel, zonesLabel, zonesValueLabel, sceneLabel, sceneValueLabel); [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[headerView]-[acresLabel]" options:0 metrics:nil views:views]] ; [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[acresLabel]-[addedLabel(==acresLabel)]-[typeLabel(==acresLabel)]-[zonesLabel(==acresLabel)]-[sceneLabel(==acresLabel)]-|" options:NSLayoutFormatAlignAllRight metrics:0 views:views]]; [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[acresValueLabel]-[addedValueLabel(==acresLabel)]-[typeValueLabel(==acresLabel)]-[zonesValueLabel(==acresLabel)]-[sceneValueLabel(==acresLabel)]-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:views]]; [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-20-[acresLabel]-[acresValueLabel]" options:0 metrics:nil views:views]]; return contentView; } -(UIView *)createHeaderView { UIView *view = [UIView new]; view.translatesAutoresizingMaskIntoConstraints = FALSE; view.backgroundColor = [UIColor blueColor]; view.clipsToBounds = YES; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view(30)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]]; UILabel *title = [UILabel new]; title.translatesAutoresizingMaskIntoConstraints = FALSE; title.text = @"Default text"; title.font = [UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:14]; title.textColor = [UIColor whiteColor]; title.backgroundColor = [UIColor clearColor]; self.headerLabel = title; [view addSubview:title]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[title]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(title)]]; [view addConstraint:[NSLayoutConstraint constraintWithItem:title attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]]; self.headerGradient = [UIColor grayGradient]; self.headerGradient.frame = CGRectMake(0, 0, 360, 30); [view.layer insertSublayer:self.headerGradient atIndex:0]; return view; } -(UILabel *)createLabelWithTitle:(NSString *)title andFont:(UIFont *)font; { UILabel *label = [UILabel new]; label.translatesAutoresizingMaskIntoConstraints = FALSE; label.text = title; label.font = font; label.textAlignment = NSTextAlignmentRight; label.textColor = [UIColor whiteColor]; label.backgroundColor = [UIColor clearColor]; return label; } 

这里是我的UICollectionViewCell文件,我只是添加了一个DatasetFilterListPanelView到它。

 @implementation DatasetViewCell - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self addSubview:[[DatasetFilterListPanelView alloc] initWithFrame:CGRectMake(0, 0, 360, 160)]]; } return self; } 

当我在UIScrollview中使用相同的面板时,一旦它们全部加载并定位,它将平滑滚动。 所以它必须是加载UICollectionView的单元需求方面。

我遵循这个UICollectionView教程

编辑:创build单元格:

 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { DatasetViewCell *datasetCell = [collectionView dequeueReusableCellWithReuseIdentifier:DatasetCellIdentifier forIndexPath:indexPath]; return datasetCell; } 

编辑2:仪器跟踪:

在这里输入图像说明

在这里输入图像说明

在这里输入图像说明

好吧,经过多次玩耍,我find了罪魁祸首:约束! CodaFI是对的。 我没有那么多的限制,所以我不认为这可能是问题。

我创build了一个笔尖文件,并删除了自动布局,现在它顺利滚动。

一天的课程:约束计算起来很慢!

通常问题是你不重用单元格。 确保使用dequeueReusableCellWithReuseIdentifier:forIndexPath:重用现有的单元格。