2012-11-19 45 views
0

我有一個應用程序,其中我從3種類型的定製cells中加載UITableView。我爲此做了3個自定義類,並通過使用佈局subview方法添加了所有元素。但問題是所有的3都有一個文本視圖。它可以是可變的。之後,我需要添加一個按鈕和一個label文本查看內容。我可以做,但如何根據內容視圖大小增加單元格的大小?如何從自定義單元格加載時調整單元格表的高度?

+0

可能重複[如何動態調整的UITableViewCell高度(http://stackoverflow.com/questions/3069339/how-to-dynamically- resize-uitableviewcell-height) –

回答

2

請檢查自定義單元格高度將與表格視圖中每個單元格高度相同。

您可以使用以下功能調整表格視圖的單元高度。

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

您可以使用這樣的代碼。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Defination *definition = [self.dataSource objectAtIndex:indexPath.row]; 
    NSString *detailString = definition.detailString; 
    CGSize detailSize; 
    detailSize = [detailString sizeWithFont:fontSize(12.0) constrainedToSize:CGSizeMake(420, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; 

    return detailSize.height + 20.0; 
} 
+0

我知道,我需要如何通過檢查文本查看內容來添加高度 –

+0

@angry_developer上面的代碼是否可以解決您的問題? –

+0

我一點都沒有得到你嗎? –

0

您可以通過以下功能

-(CGSize)SizeOfString:(NSString *)string withFont:(UIFont *)font constrainedToWidth:(CGFloat)width 
{ 
    CGSize size = [string sizeWithFont:font constrainedToSize:CGSizeMake(width, 4000) lineBreakMode:UILineBreakModeWordWrap]; 
    return CGSizeMake(width, size.height + 10); 
} 

呼叫

CGSize size = [self SizeOfString:your_text withFont:your_font constrainedToWidth:width_of_showing_area]; 
0

生病添加測量更復雜的細胞的另一個例子計算高度。 我心中測量代碼心不是100%正確的,但它顯示的方法

+ (CGFloat)calculatedCellHeightForTweet:(id)tweet width:(CGFloat)width { 
//text 
CGSize s1 = [tweet[@"text"] sizeWithFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]] 
         constrainedToSize:CGSizeMake(width, MAXFLOAT) 
          lineBreakMode:NSLineBreakByWordWrapping]; 

//user 
NSString *text; 
if(![tweet[@"to_user"] isKindOfClass:[NSNull class]]) 
    text = [NSString stringWithFormat:@"%@ > %@", tweet[@"from_user"], tweet[@"to_user"]]; 
else 
    text = tweet[@"from_user"]; 
CGSize s2 = [text sizeWithFont:[UIFont boldSystemFontOfSize:[UIFont labelFontSize]] 
         forWidth:width 
       lineBreakMode:NSLineBreakByWordWrapping]; 

return fmaxf(s1.height + s2.height + /*padding*/ 44, 60); 
} 
相關問題