2014-11-21 13 views
0

我很努力如何在uitableviewcell内动态调整uilabel的大小,并根据我放入标签的文本调整表格单元格的高度。我想在tableview单元格中显示一个完整的字符串,对于所有的单元格总是以相同的字体大小显示。这看起来很标准,但我注意到很多讨论/辩论/对stackoverflow的不满。我开始与这些职位:动态调整大小的多行UILabel和附带的表格视图单元编程方式

Resize Custom cell with a UILabel on it based on content text from JSON file(@塞牙的答案) boundingRectWithSize for NSAttributedString returning wrong size(@ JOM的答案)

我试图使用的功能是什么塞牙没有副本,几乎没有修改。但是,我发现虽然该功能为不同的单元格打印出不同的高度,但我看到的每个标签都有1行文本,即使它们应该显示很多行。另外,奇怪的是,一个单元格的文本似乎显示在另一个单元格的顶部 - 不确定这是否相关。

我的2个问题:(1)为什么我只看到第一行文字? (2)为什么UILabel/cell没有按照我在日志中打印的不同高度来调整大小(或者它们调整大小,但是被问题#1掩盖了)?

这里的三种功能,我使用(这些连同厦门国际银行 - 可以在厦门国际银行是什么问题?):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
JudgeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JudgeCell"]; 
    if(!cell) 
    { 
     [tableView registerNib:[UINib nibWithNibName:@"JudgeCell" bundle:nil] forCellReuseIdentifier:@"JudgeCell"]; 
     cell = [tableView dequeueReusableCellWithIdentifier:@"JudgeCell"]; 
    } 

    cell.username.text = self.object.answerUser[indexPath.row]; 
    cell.answer.text = self.object.answerArray[indexPath.row]; 

    NSString *text = cell.answer.text; 
    CGSize constraint = CGSizeMake(50, 20000.0f); 
    CGSize size = [self frameForText:text sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:constraint]; 

    cell.username.frame = CGRectMake(10, 10, 50, size.height); //MAX(size.height, 14.0f)); 

    return cell; 

} 

-(CGSize)frameForText:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size { 

    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy]; 
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping; 

    NSDictionary * attributes = @{NSFontAttributeName:font, 
            NSParagraphStyleAttributeName:paragraphStyle 
            }; 


    CGRect textRect = [text boundingRectWithSize:size 
             options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) 
             attributes:attributes 
             context:nil]; 

    NSLog(@"size is %f ", textRect.size.height); 
    return textRect.size; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    NSString *text = self.object.answerArray[indexPath.row]; 
    CGSize constraint = CGSizeMake(50, 20000.0f); 
    CGSize size = [self frameForText:text sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:constraint]; 
    CGFloat height = size.height; //MAX(size.height, 14.0f); 
    NSLog(@"size is %f AT INDEX %d", height, indexPath.row); 
    return height + 20; 
} 

感谢您的咨询!

+0

您是否尝试致电[cell setNeedsDisplay]?在cellForRowAtIndexPath中设置新大小后? – Priyatham51 2014-11-21 00:48:46

+0

@ Priyatham51是的,我也试过,但没有奏效。 – 2014-11-21 16:12:11

回答

0

最后,我删除了相关的笔尖文件,并以某种方式,似乎这样的伎俩与本教程中的代码(这是类似于我上面已经贴)沿:

http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/

请注意,本教程中的方法有时会被标记为已弃用。如果我找到更新的教程,我会发布。

相关问题