我有一个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将位于单元格的底部,而第一个名称位于中间。
我做错了什么?我已经在这几个小时了。
感谢您的帮助。