多くの異なる内で使用する予定の UILabel をサブクラス化しましたUITableViewCell
。
内で、の動的な高さを取得するためにsetText:
使用しました。sizeWithFont
UILabel
これはすべてうまく機能UILabel
しますが、セルが再描画されるまでサブクラスのフレームは変更されません。どうすればこれを克服できますか?
注:if
以下のコードでは、ステートメントのため、再描画されてもサイズは変わりません。これをテスト用に取り出したところ、再描画後に機能することがわかりました。
または類似の使用が必要だと思いましたが、setNeedsDisplay
どちらも機能しません。
私が使用しているコードは次のとおりです。
- (void)setText:(NSString *)text
{
if (text != _text) {
// Adjust size to fit contents
//
CGSize maximumSize = CGSizeMake(CGRectGetWidth(self.frame), CGFLOAT_MAX);
CGSize predictedSize = [text sizeWithFont:self.font constrainedToSize:maximumSize lineBreakMode:self.lineBreakMode];
NSLog(@"Predicted size for \"%@\" (width: %f) is %@", text, self.frame.size.width, NSStringFromCGSize(predictedSize));
CGRect headlineDescriptionLabelFrame = self.frame;
headlineDescriptionLabelFrame.size = predictedSize;
NSLog(@"Old frame: %@", NSStringFromCGRect(self.frame));
[self setFrame:headlineDescriptionLabelFrame];
NSLog(@"New frame: %@", NSStringFromCGRect(self.frame));
_text = text;
}
}
私の出力は次のようになります(上記の「if」がオフになっています):
2012-12-19 19:51:23.576 myApp[1099:c07] Predicted size for "Facebook's photo-sharing service Instagram denies that it has changed its privacy policy to allow it to sell users' photos to advertisers." (width: 172.000000) is {170, 75}
2012-12-19 19:51:23.578 myApp[1099:c07] Old frame: {{138, 46}, {172, 49}}
2012-12-19 19:51:23.578 myApp[1099:c07] New frame: {{138, 46}, {170, 75}}
ここでセルをリロードします
UILabel はサイズ変更され、この最初のセルのリロード後に表示されます
2012-12-19 19:51:30.018 myApp[1099:c07] Predicted size for "Facebook's photo-sharing service Instagram denies that it has changed its privacy policy to allow it to sell users' photos to advertisers." (width: 172.000000) is {170, 75}
2012-12-19 19:51:30.018 myApp[1099:c07] Old frame: {{138, 46}, {172, 49}}
2012-12-19 19:51:30.019 myApp[1099:c07] New frame: {{138, 46}, {170, 75}}
ここでセルをリロード
2012-12-19 19:51:32.014 myApp[1099:c07] Predicted size for "Facebook's photo-sharing service Instagram denies that it has changed its privacy policy to allow it to sell users' photos to advertisers." (width: 170.000000) is {170, 75}
2012-12-19 19:51:32.014 myApp[1099:c07] Old frame: {{138, 46}, {170, 75}}
2012-12-19 19:51:32.015 myApp[1099:c07] New frame: {{138, 46}, {170, 75}}
編集
それを理解しようとして4日経った後、私はまだこれに問題があります.これがApple側のバグであるかどうか誰かが知っていますか? 他の人は、UITableViewCell nib から読み込まれた UILabel のサイズをどのように変更しますcellForRowAtIndexPath:
か?