我创建了一个UICollectionViewCell
包含标签出口的自定义(放置在情节提要中)。我想从awakeFromNib
我的自定义方法中获取此标签的高度UICollectionViewCell
,但标签的大小始终为0.000000
:
// .h
@interface MyCustomCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@end
// .m
@implementation MyCustomCell
-(void)awakeFromNib
{
[super awakeFromNib];
NSLog(@"%@", self.myLabel);
NSLog(@"%f", self.myLabel.frame.size.width);
NSLog(@"%f", self.myLabel.frame.size.height);
}
@end
控制台输出:
2013-02-06 11:13:59.628 Test[8880:c07] <UILabel: 0x7649a00; frame = (0 0; 0 0); text = '12h00'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7649ac0>>
2013-02-06 11:13:59.628 Test[8880:c07] 0.000000
2013-02-06 11:13:59.630 Test[8880:c07] 0.000000
如何获得标签的大小?awakeFromNib
在被调用时获得此类信息是否为时过早?如果是这样,我应该以我的自定义单元格的哪种方法获得大小?
编辑
这是在 ViewController 中观察到的同样奇怪的行为:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCustomCell" forIndexPath:indexPath];
NSLog(@"%@", cell);
NSLog(@"%@", cell.myLabel);
}
和输出:
2013-02-07 16:07:34.488 Test[30308:c07] <MyCustomCell: 0x7156980; baseClass = UICollectionViewCell; frame = (20 0; 290 655); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7156a80>>
2013-02-07 16:07:34.489 Test[30308:c07] <UILabel: 0x7158100; frame = (0 0; 0 0); text = 'this is a text'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7158040>>