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>>