【问题标题】:Dynamically change UITable cell height?动态改变 UITable 单元格高度?
【发布时间】:2012-10-22 19:52:55
【问题描述】:

我需要根据内容大小/长度调整单元格的高度。尝试了几种方法,哪一种给出了准确的高度而不重叠?

【问题讨论】:

  • 检查我在这里给出的答案stackoverflow.com/a/12600584/1538079
  • 你得到了对实施有用的答案吗?
  • 是的..自定义 UITableViewCell 的高度.. 这对我有帮助
  • -(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth { CGSize maximumSize = CGSizeMake(labelWidth, 10000); //提供合适的字体和字号 CGSize labelHeighSize = [text sizeWithFont: [UIFont fontWithName:@"Trebuchet MS" size:13.0f] constrainedToSize:maximumSize lineBreakMode:UILineBreakModeTailTruncation];返回标签HeighSize.height; }
  • @RamkumarThiyyakat 查看 Foram Mukund Shah 的帖子

标签: iphone ios ipad ios-simulator


【解决方案1】:

这是我之前的回答 - Customizing UITableViewCell's height:

使用此方法获取文字的文字高度

-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
{

CGSize maximumSize = CGSizeMake(labelWidth, 10000);

//provide appropriate font and font size
CGSize labelHeighSize = [text sizeWithFont: [UIFont fontWithName:@"Trebuchet MS" size:13.0f]
                         constrainedToSize:maximumSize
                             lineBreakMode:UILineBreakModeTailTruncation];
return labelHeighSize.height;
}

此方法将返回您传递的文本的高度。在您的课程中添加此方法。并使用 tableView:heightForRowAtIndexPath: 委托方法设置每个单元格的高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   Feedback *item = [self.items objectAtIndex:indexPath.row];

   CGFloat textHeight = [self getLabelHeightForText:item.comment andWidth:162];//give your label width here
    return textHeight;
}    

【讨论】:

  • 反馈*item = [self.items objectAtIndex:indexPath.row];
  • 这是什么意思?你能解释一下吗?我的意思是反馈。
  • 这就是他问题的答案。反馈是一个对象,它存储数组中的值。
  • 嘿,什么是反馈?它是 urs 的一类吗?我可以知道你在那门课上都做了什么吗
【解决方案2】:

请参阅本教程以更改 UITableViewCell 动态高度..

Resizing-A-UITableViewCell

也可以使用本教程..

uitableviewcell-dynamic-height

也可以使用 tableView:heightForRowAtIndexPath: 方法来设置单元格的高度,indexpath

【讨论】:

    【解决方案3】:
    //Calculate the expected size based on the font and linebreak mode of your label
    CGSize maximumLabelSize = CGSizeMake(296,9999);
    
    CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];   
    
    //adjust the label the the new height.
    CGRect newFrame = yourLabel.frame;
    newFrame.size.height = expectedLabelSize.height;
    yourLabel.frame = newFrame;
    

    【讨论】:

      【解决方案4】:

      您可以编辑和尝试的示例

       - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath         *)indexPath {
      
      NSDictionary *itemAtIndex = (NSDictionary *)[chatQuestions objectAtIndex:indexPath.row];
      
      if ([self sizeForText:[itemAtIndex objectForKey:@"text"]].height+20>50) {
          return [self sizeForText:[itemAtIndex objectForKey:@"text"]].height+20;
      }
      else{
          return 50;}
      
      
      
      }
      
       -(CGSize)sizeForText:(NSString*)text
        {
      
      
      CGSize constraintSize;
      
      constraintSize.width = 190.0f;
      
      constraintSize.height = MAXFLOAT;
      UIFont *labelFont = [UIFont fontWithName:@"Noteworthy-Light" size:18];
      
      CGSize stringSize =[text sizeWithFont:labelFont constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
      
      return stringSize;
      }
      

      【讨论】:

        【解决方案5】:

        在您的情况下,您需要根据标签设置单元格的高度。

        检查链接:

        http://dcraziee.wordpress.com/2013/05/22/calculate-size-of-uillabel-base-on-text-in/

        有一个函数名为

        -(CGFloat)getHeightForLabel:(NSString *)_str font:(UIFont *)fontOfObject
        

        使用该函数计算高度为:

        -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            CGFloat _height  = [self getHeightForLabel:self.label.text font:[self.label font]];
            return _height;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-14
          • 2014-06-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多