简体   繁体   中英

Multiple UITableViewCell types implementation

I am creating a custom cell class wherein I am putting different types of subviews in my init method but with frame as CGRectZero.

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];

In my layoutSubviews method, I am using these labels based on my need. Like, for one type of cell I will use first label and for other type will use another label.

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));
    }

My question is that is it a good idea to do this from memory perspective. As though my purpose is resolved but my custom cell object contains sub views which are in the memory but not visible. If yes, then what is the alternate approach for this kinda scenario.

If not all of the subviews are going to get used for each cell type, I suggest lazily creating each subview when it is first accessed. This could be accomplished by overriding its getter like this:

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

This way, subTitleLabel is created and added to the content view the first time it is accessed through its getter. If you never call the getter, eg, because the type of cell doesn't require it, self.subTitleLabel will never get called and subTitleLabel will never get created.

I wouldn't think memory issues here would be that big of a problem. If you are correctly reusing cells then you probably only create 5-10 cells in your list that are constantly recycled.

Though one alternative solution would be to use the celltype as the reuse identifier and only create the subviews that are needed for that type. This way when you get a cell for a particular type you know it will have only the required fields. This could create more cells though which may actually take up more memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM