使用UIImage和UILabels创建UICollectionViewCell会创建慢速滚动

时间:2013-03-01 17:40:32

标签: ios objective-c

我使用UIview(充当标题),UIImage和10个UILabel创建子视图。我将它们作为单元格放入UICollectionView中。

完全设计后,它不能平滑滚动。如果我删除所有UILabel,它会顺利滚动。

我认为它是缓慢的,因为UICollectionView按需加载,所以当它需要每个新单元时,它必须绘制它来锁定主线程。

对于iOS来说,创建它们的过程是否过分?如果是这样,我可以用另一种方式将文本放入其中吗?

我的细胞看起来像什么:

enter image description here

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

@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文件,我只是addSubview一个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的按需单元格。

我关注this UICollectionView Tutorial

编辑:创建单元格:

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

    return datasetCell;
}

编辑2:仪器追踪:

enter image description here

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:4)

好的,经过多次游戏,我找到了罪魁祸首:限制! CodaFI是对的。我在小组中没有那么多限制因此我认为这不是问题所在。

我创建了一个nib文件并删除了autolayout,它现在可以顺利滚动。

当天的课程:约束计算速度很慢!

答案 1 :(得分:1)

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