Search code examples
iphoneiosuitableviewautoresizingmask

How to use autoresizingmask to stop components of UITableViewCell disappearing when editing? (iPhone/iPad)


I have a custom UITableViewCell which contains 3 UILabels. Two of these labels are aligned with the left hand side of the cell, the other one is aligned on the right hand side. Here is what it looks like:

enter image description here

The issue I'm having is when the UITableView is in editing mode. I have been trying to stop the labels on the right from disappearing off the side of the screen through the use of AutoResizingMasks but I'm not having any luck. Here is what happens:

enter image description here

The code I have been playing around with comes from my subclasses UITableViewCell. Here is my code:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) 
    {
        // Setup the cells accessory
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        // Setup the cell background
        self.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"Plain_Cell.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];

        // Setup the Main label (fruit names)
        mainLabel = [[UILabel alloc] init];
        mainLabel.font = [UIFont systemFontOfSize:25];
        mainLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:mainLabel];

        // Setup the dates label
        datesLabel = [[UILabel alloc] init];
        datesLabel.font = [UIFont systemFontOfSize:14];
        datesLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:datesLabel];

        // Setup the milage label
        distanceLabel = [[UILabel alloc] init];
        distanceLabel.textAlignment = UITextAlignmentRight;
        distanceLabel.font = [UIFont systemFontOfSize:14];
        distanceLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:distanceLabel];
    }

    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];

    mainLabel.frame = CGRectMake(10, 10, 280, 39);
    datesLabel.frame = CGRectMake(10, 58, 184, 21);
    distanceLabel.frame = CGRectMake(205, 58, 85, 21);

    mainLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
    datesLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
    distanceLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
}

NOTE: Please scroll down for more code!

I haven't had much luck with it though. Please can someone help me out?


Solution

  • You are aligning your text to the right, but not the label. Try this:

    ...
    // Setup the mileage label
    distanceLabel = [[UILabel alloc] init];
    distanceLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    distanceLabel.textAlignment = UITextAlignmentRight;
    distanceLabel.font = [UIFont systemFontOfSize:14];
    distanceLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:distanceLabel];
    

    This will lock the right margin where it is and allow the left margin to shrink when the view shrinks.