2014-01-06 56 views
0

我使用xib創建自定義單元格。我想顯示一個標籤,取決於標籤文本。如何增加行高以及標籤高度?如何增加iPhone的行高?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    customCell *cell=(customCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[NSBundle mainBundle]loadNibNamed:@"customCell" owner:self options:nil]objectAtIndex:0]; 
     UIImage *userimage=[self getUserImage:[userimageURLS objectAtIndex:indexPath.row]]; 
     cell.userImageViewIv.autoresizesSubviews=YES; 
     cell.userImageViewIv.image=userimage; 
     cell.userNameLbl.text=[UserName objectAtIndex:indexPath.row]; 
     cell.commenttxtlbl.text=[postText objectAtIndex:indexPath.row]; 
     NSLog(@"cell created"); 
    } 
    return cell; 
} 

變量posttext數組不是靜態的。它是動態的。

我已經使用了上面的代碼。

+0

你在heightForCell中寫了什麼? – Nirmalsinh

+1

如果> = iso7參考 ** - tableView:estimatedHeightForRowAtIndexPath:** https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference。html#// apple_ref/occ/intfm/UITableViewDelegate/tableView:estimatedHeightForRowAtIndexPath: – CoolMonster

回答

0

動態

- (CGFloat)tableView:(UITableView *)tableView 
    heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *attribValue = stringToBeWrittenInTheCell; 

    CGFloat labelWidth = WidthOfTheCellLabel; 

    titlelblSize = [attribValue 
    sizeWithFont:[UIFont 
    boldSystemFontOfSize: FontSize 
    constrainedToSize:CGSizeMake(labelWidth,labelHeight) 
    lineBreakMode:UILineBreakModeWordWrap]; 

    [attribValue release]; 
    attribValue = nil; 

    return titlelblSize.height; 

} 

靜態

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

或使用情節提要/十四

enter image description here

0

您可以通過兩種方式做到這一點....

1.

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

2.

[tableview setRowHeight:80]; 
+0

他要求動態高度。 –

1
CGSize textSize = [myText sizeWithFont: LableFont 
         constrainedToSize:CGSizeMake(LABLE_WIDTH, CGFLOAT_MAX) 
          lineBreakMode:UILineBreakModeWordWrap]; 

利用這個來計算的大小需要爲您的標籤,然後爲標籤設置新的框架。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CGSize textSize = [myText sizeWithFont: LableFont 
          constrainedToSize:CGSizeMake(LABLE_WIDTH, CGFLOAT_MAX) 
           lineBreakMode:UILineBreakModeWordWrap]; 
return textsize.height+somefixvalue; 
} 
+0

constrainedToSize已被棄用,您應該使用:boundingRectWithSize:options:attributes:context: – YoCoh

0

要得到正確的高度使用NSString方法-boundingRectWithSize:選項:屬性:背景:

使用它在兩個地方:的cellForRowAtIndexPath和heightForRowAtIndexPath。

在cellForRowAtIndexPath中,將標籤的幀高設置爲從此indexPath的特定文本上運行的此方法檢索的高度。

在heightForRowAtIndexPath中,使用相同的方法,使用在此indexPath的特定文本上運行的相同方法設置高度。

0

我感謝所有的你,但有一件事sizeWithFont現已棄用在IOS 7. IOS 7只是做以下

-(CGRect)sizeOfText:(NSString *)str 
{ 
    return [str boundingRectWithSize:CGSizeMake(LABEL_WIDTH, LABEL_MAX_HEIGHT) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"YourLabelFontName" size:16.0f]} context:Nil]; //instead of 16.0f put your Label font size 
} 


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // include the below code with yours 
    CGRect stringRect=[self sizeOfText:(NSString *)[postText objectAtIndex:[indexPath row]]]; 
    float labelHeight=stringRect.size.height; 
    return labelHeight + 100.0f; // here 100.0f is default change it according to your cell height 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // include the below code with yours 
    NSString *commentString=[postText objectAtIndex:indexPath.row]; 
    CGRect stringRect=[self sizeOfText:commentString]; 
    cell.commenttxtlbl.text=commentString; 
    CGRect newRect=CGRectMake([cell.commenttxtlbl frame].origin.x, [cell.commenttxtlbl frame].origin.x, stringRect.size.width, stringRect.size.height); 
    [cell.commenttxtlbl setFrame:newRect]; 
} 

希望能對大家有所幫助所有的你..

0

嘗試這些

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    MessageConversation *message = [marrMessage objectAtIndex:indexPath.row]; 

    CGSize szMaxCell = CGSizeMake(220, 2999); 
    UIFont *font = [UIFont systemFontOfSize:14.0f]; // whatever font you're using to display 
    CGSize szCell = [message.message sizeWithFont:font constrainedToSize:szMaxCell lineBreakMode:NSLineBreakByWordWrapping]; 
// NSLog(@"szCell %@",NSStringFromCGSize(szCell)); 
    if(szCell.height > 120){ 
     return szCell.height+100; 
    } 
    return 150; 
} 

而且使用相同的的cellForRowAtIndexPath

rightCell.lblMessage.numberOfLines = 0; 
CGSize szMaxCell = CGSizeMake(220, 2999); 
UIFont *font = [UIFont systemFontOfSize:17.0f]; // whatever font you're using to display 
CGSize szCell = [message.message sizeWithFont:font constrainedToSize:szMaxCell lineBreakMode:NSLineBreakByCharWrapping]; 

CGRect lblFrame = rightCell.lblMessage.frame; 
lblFrame.size.height = szCell.height; 
rightCell.lblMessage.frame = lblFrame; 

用lblMessage替換您的標籤名稱。你就完成了。