0

我有一个包含 3 个 UILabel 的自定义 UITableViewCell。其中两个标签与单元格的左侧对齐,另一个与右侧对齐。这是它的样子:

在此处输入图像描述

我遇到的问题是 UITableView 处于编辑模式时。我一直试图通过使用 AutoResizingMasks 来阻止右侧的标签从屏幕一侧消失,但我没有任何运气。这是发生的事情:

在此处输入图像描述

我一直在玩的代码来自我的子类 UITableViewCell。这是我的代码:

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

注意:请向下滚动以获取更多代码!

不过,我没有太多运气。请问有人可以帮我吗?

4

1 回答 1

2

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.

于 2012-05-31T21:29:55.597 回答