代码之家  ›  专栏  ›  技术社区  ›  Abhinav

多个UITableViewCell类型实现

  •  1
  • Abhinav  · 技术社区  · 14 年前

    我正在创建一个自定义单元类,其中我在init方法中放置了不同类型的子视图,但frame为cgritzero。

    self.subTitleLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];[self.contentView addSubview:self.subTitleLabel];
    
    self.scannedProductLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
    [self.contentView addSubview:self.scannedProductLabel];
    
    self.requestStatusLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
    [self.contentView addSubview:self.requestStatusLabel];
    

    在我的layoutSubviews方法中,我根据需要使用这些标签。例如,对于一种类型的单元格,我将使用第一个标签,而对于另一种类型,将使用另一个标签。

    if ([self.cellType isEqualToString:@"CustomerDetails"] ) {
            //self.productImageView.frame = CGRectMake(aContentRect.origin.x + kCellOffset, 0.0f, aTitleCellWidth , floorf(aHeight/4));
            self.titleLabel.frame = CGRectMake(aContentRect.origin.x + kCellOffset, 0.0f, aTitleCellWidth , floorf(aHeight/2));
            self.subTitleLabel.frame = CGRectMake(aContentRect.origin.x + kCellOffset, floorf(aHeight/2), aTitleCellWidth, floorf(aHeight/4));
            self.requestStatusLabel.frame = CGRectMake(aContentRect.origin.x + kCellOffset, floorf((aHeight/2) + (aHeight/4)), aTitleCellWidth , floorf(aHeight/4));
        }
    

    我的问题是,从记忆的角度来看,这样做是个好主意。好像我的目的已经解决了,但是我的自定义单元格对象包含内存中不可见的子视图。如果是,那么这种情况下的替代方法是什么?

    2 回复  |  直到 14 年前
        1
  •  0
  •   James Huddleston    14 年前

    - (UILabel)subTitleLabel {
        if (subTitleLabel == nil) {
            subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kSubTitleX, kSubTitleY, kSubTitleWidth, kSubTitleHeight)];
            [self.contentView addSubview:subTitleLabel];
        }
        return subTitleLabel;
    }
    

    subTitleLabel self.subTitleLabel

        2
  •  0
  •   skorulis    14 年前