Iphone 动态更改可用单元格高度?

Iphone 动态更改可用单元格高度?,iphone,ios,ipad,ios-simulator,Iphone,Ios,Ipad,Ios Simulator,我需要根据内容大小/长度调整单元格高度。我尝试了几种方法,哪种方法可以提供准确的高度而不重叠?这是我以前的答案-: 使用此方法获取文本的文本高度 -(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth { CGSize maximumSize = CGSizeMake(labelWidth, 10000); //provide appropriate font and font size CG

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

这是我以前的答案-:

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

-(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;
}    

有关动态更改
UITableViewCell
高度的信息,请参见本教程

并使用本教程


还可以使用tableView:heightForRowAtIndexPath:方法,通过
indepath
示例设置单元格高度,您可以编辑并尝试

//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;
 - (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;
}

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

检查链接:

有一个函数名为

-(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;
}

检查我在这里给出的答案你得到了对实现有用的答案吗?是的..自定义UITableViewCell的高度。。这有助于我-(CGFloat)获取LabelHeightForText:(NSString*)文本和宽度:(CGFloat)labelWidth{CGSize maximumSize=CGSizeMake(labelWidth,10000);//提供适当的字体和字体大小CGSize labelHeighSize=[text-sizeWithFont:[UIFontWithName:@“Trebuchet MS”大小:13.0f]constrainedToSize:maximumSize lineBreakMode:UILineBreakModeTailTruncation];返回labelHeighSize.height;}@RamkumArthiyakat通过Foram Mukund ShahFeedback检查帖子*项=[self.items objectAtIndex:indexath.row];这是什么意思?你能解释一下吗?我指的是反馈。这就是他的问题的答案。反馈是一个对象,它存储来自数组的值。嘿,什么是反馈?它是一类urs吗?我可以知道你在那门课上都做些什么吗