3

のための動的身長私はiOS9 XCode7 を使用しています、私は私が使用しているlabelText身長のiOS:UITableViewCellの

に応じて動的にセルの高さを変更する必要があります。

self.tableView.rowHeight=UITableViewAutomaticDimension; 

しかし、それはありませんカスタムメイドのセルのために働いています。 sizetoFitがiOS9で削除されました。

何かお勧めします。 ありがとう

+0

あなたが自動レイアウト使用していますか? – Joshua

+1

推定された行の高さも指定する必要があります。 self.tableView.estimatedRowHeight = 100と指定します。 –

答えて

8

セルの上、下、左、右を基準にラベルの制約を指定します。

セルのサイズがコンテンツの高さとともに大きくなります。

またあなたのラベルの複数行を作る(属性インスペクタで0に行プロパティを設定することにより)

#pragma mark- Menu table Delegates 

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    return UITableViewAutomaticDimension; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    return UITableViewAutomaticDimension; 
} 
1

を私は何この文は、手段(スクリーンショットを助けただろう)かなりよく分からない:。

は、そのセルのラベルに応じて高さを持っているあなたのテーブルには、

だから、あなたはUITableViewCellを持っている「私は動的にlabelText高さに応じてセルの高さを変更する必要がある」UILabelを含む、各セルをしたいですか?

私が使用するコードは次のとおりです。私UITableViewCellクラスで

、私は測定するために、静的な関数を定義し、私の.xibファイルの高さを返します:

@implementation MikesTableViewCell 

... 

+(CGSize)preferredSize 
{ 
    static NSValue *sizeBox = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     // Assumption: The XIB file name matches this UIView subclass name. 
     NSString* nibName = NSStringFromClass(self); 
     UINib *nib = [UINib nibWithNibName:nibName bundle:nil]; 

     // Assumption: The XIB file only contains a single root UIView. 
     UIView *rootView = [[nib instantiateWithOwner:nil options:nil] lastObject]; 

     sizeBox = [NSValue valueWithCGSize:rootView.frame.size]; 
    }); 
    return [sizeBox CGSizeValue]; 
} 

@end 

その後、私のUIViewControllerクラスでは、私は私のUITableViewまでを教えてあげますこのセルを使用して、高さを調べる。

@property (nonatomic) float rowHeight; 

UINib *cellNib = [UINib nibWithNibName:@"MikesTableViewCell" bundle:nil]; 
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"CustomerCell"]; 

rowHeight = [MikesTableViewCell preferredSize].height; 

... 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return (float)rowHeight; 
} 

また、これはあなたが探しているものと正確ではないかもしれませんが、これが役立つことを願っています。

1

これを試してください。 それはとても簡単です。

セットheightForRowAtIndexPath方法で、この高さ

float vendorNameHeight = [vendorNameLbl.text heigthWithWidth:width andFont:[UIFont fontWithName:@"Signika-Light" size:21.0]]; 

その後、

UILabel*vendorNameLbl=[[UILabel alloc]initWithFrame:CGRectMake(13, 11, 100, 22)]; 
    vendorNameLbl.numberOfLines = 0; 
    [vendorNameLbl sizeToFitVertically]; 
関連する問題