Dynamic UILabel在LayoutSubview中错误调整大小

Dynamic UILabel在LayoutSubview中错误调整大小,dynamic,uilabel,layoutsubviews,Dynamic,Uilabel,Layoutsubviews,我有一个UITableViewCell的子类来显示自定义单元格。此类与故事板集成,因此属性的框架已在IB中设置 我的目标是根据标签包含的文本动态设置标签的宽度,并使用setNeedsLayout重新绘制标签 自定义单元格的.m文件的一部分: static CGFloat const kHorizontalEdgeOffset = 74.0; static CGFloat const kVerticalEdgeOffset = 10.0; static CGFloat const kNameLab

我有一个UITableViewCell的子类来显示自定义单元格。此类与故事板集成,因此属性的框架已在IB中设置

我的目标是根据标签包含的文本动态设置标签的宽度,并使用setNeedsLayout重新绘制标签

自定义单元格的.m文件的一部分:

static CGFloat const kHorizontalEdgeOffset = 74.0;
static CGFloat const kVerticalEdgeOffset = 10.0;
static CGFloat const kNameLabelHeight = 22.0;

- (void)setFriend:(Person *)friend
{
    if (_friend != freeDomeFriend) {
        _friend = friend;
        self.friend = friend;
    }

    // Set the text for the first name and save the size to fit the text.
    self.firstNameLabel.text = friend.firstName;
    self.firstNameLabelSize = [self.firstNameLabel.text sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:17.0]];
    // Set the text for the last name and save the size to fit the text.
    self.lastNameLabel.text = friend.lastName;
    self.lastNameLabelSize = [self.lastNameLabel.text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17.0]];

    // Ask the view to layout the view with the changes.
    [self setNeedsLayout];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)layoutSubviews
{    
    [super layoutSubviews];

    // Set the new frames according to the caluclated size for the labels.
    self.firstNameLabel.frame = CGRectMake(kHorizontalEdgeOffset, kVerticalEdgeOffset, kNameLabelHeight, self.firstNameLabelSize.width);
    self.lastNameLabel.frame = CGRectMake(kHorizontalEdgeOffset + self.firstNameLabelSize.width + 2.0,
                                          kVerticalEdgeOffset, kNameLabelHeight, self.lastNameLabelSize.width);

    NSLog(@"FN: %@ \n LN: %@ \n ------------------", self.firstNameLabel, self.lastNameLabel);
}
我的NSLog为我提供了正确的输出:

2012-05-12 23:56:36.741 MyProject[11421:16403] FN: <UILabel: 0xa888eb0; frame = (74 10; 22 57); text = 'Callum'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa880fa0>> 
 LN: <UILabel: 0xa8890c0; frame = (133 10; 22 45); text = 'Webb'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa889130>> 
 ------------------
2012-05-12 23:56:36.742 MyProject[11421:16403] FN: <UILabel: 0xa88abf0; frame = (74 10; 22 48); text = 'Hervé'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa88ac60>> 
 LN: <UILabel: 0xa88ad00; frame = (124 10; 22 86); text = 'Fahendrich'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa88ad70>> 
 ------------------
2012-05-12 23:56:36.743 MyProject[11421:16403] FN: <UILabel: 0xa88b4a0; frame = (74 10; 22 41); text = 'John'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa88b510>> 
 LN: <UILabel: 0xa88b5b0; frame = (117 10; 22 50); text = 'Clema'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa88b620>> 
 ------------------
2012-05-12 23:56:36.743 MyProject[11421:16403] FN: <UILabel: 0xa88bf30; frame = (74 10; 22 40); text = 'Luke'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa88bfa0>> 
 LN: <UILabel: 0xa88c040; frame = (116 10; 22 74); text = 'McManus'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa88c0b0>> 
 ------------------
2012-05-12 23:56:36.741我的项目[11421:16403]FN:
自然对数:
------------------
2012-05-12 23:56:36.742我的项目[11421:16403]FN:
自然对数:
------------------
2012-05-12 23:56:36.743我的项目[11421:16403]FN:
自然对数:
------------------
2012-05-12 23:56:36.743我的项目[11421:16403]FN:
自然对数:
------------------
然而,这是我在视觉上得到的(我无法上传图片,所以我会尽力描述它): 标签的大小(宽度和高度)非常小,并且原点似乎是随机的。有时,姓氏将位于单元格的底部,中间的第一个名字。

我做错了什么?我已经谈了好几个小时了

谢谢你的帮助