1

我有一个 UITableViewCell 的子类来显示自定义单元格。此类与 Storyboard 集成,因此属性的框架已在 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>> 
 ------------------

然而,这就是我在视觉上得到的(我无法上传图片,所以我会尽力描述它):标签的尺寸非常小(宽度和高度),并且来源似乎是随机的。有时 lastName 将位于单元格的底部,而名字则位于中间。

我究竟做错了什么?我已经做了好几个小时了。

感谢您的帮助。

4

0 回答 0