2016-10-25 146 views
0

- (CGFloat的)的tableView:(UITableView的*)的tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {延長細胞高度與labet文本的tableview細胞IOS

NSString * message =[[chatData objectAtIndex:indexPath.row]valueForKey:@"message"]; 
NSString * message_type = [[chatData objectAtIndex:indexPath.row]valueForKey:@"message_type"]; 

if([[[chatData objectAtIndex:indexPath.row]valueForKey:@"message_type"] isEqualToString:@"0"]){ 
    CGSize sizeText = [self text:message sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds) - 100, 9999) withLinebreakmode:NSLineBreakByWordWrapping]; 
    CGFloat heightOfRow = sizeText.height + 2 * 7.5f; 
    return heightOfRow + 50; 
} 
else 
    return 225;//message_type 1 3 4 

// return self.chatTable.rowHeight; 

}

編譯馬克 - 處理ChatLabel寬度

- (CGSize)文本:(的NSString *)文本sizeWithFont:(UIFont *)字體constrainedToSize:(CGSize)ConstSize withLinebreakmode:(NSLineBreakMode)linebreakmode { CGSize retsize;

if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) 
{ 
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
    paragraphStyle.lineBreakMode = linebreakmode; 
    // paragraphStyle.alignment = NSTextAlignmentLeft; 
    NSDictionary * attributes = @{NSFontAttributeName : font,NSParagraphStyleAttributeName : paragraphStyle}; 
    retsize = [text boundingRectWithSize:ConstSize options:NSStringDrawingUsesFontLeading 
       |NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; 
    retsize.height =ceilf(retsize.height); 
    retsize.width =ceilf(retsize.width); 

    NSLog(@"%f",retsize.width); 


}else{ 

    if ([text respondsToSelector:@selector(sizeWithFont:constrainedToSize:lineBreakMode:)]){ 
     retsize = [text sizeWithFont:font 
        constrainedToSize:ConstSize 
         lineBreakMode:linebreakmode]; 
     retsize = [text boundingRectWithSize:ConstSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size; 
    } 
} 
//[self viewWillAppear:YES]; 
return retsize; 

}

回答

0

試試這個代碼。

1.如果您使用此代碼,您應該只給出標籤的頂部,底部,左側和右側約束。

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //Here you give a minimum height of the cell size. 
    return 110; 
} 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewAutomaticDimension; 
}