如何以编程方式设置 Tableview CustomCell 高度? [英] How to Set Tableview CustomCell Height programmatically?

查看:18
本文介绍了如何以编程方式设置 Tableview CustomCell 高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个包含 CustomCell 的 UITableview.我的 CustomCell 包含 UITextView 和一些其他控件,但如果我的 UITextViewtext 包含一些文本,并且如果我的 TextView 文本包含空文本意味着没有任何文本,我想将我的单元格高度设置为 300,那么我希望我的单元格高度为 170.我为此编写了代码,但它不起作用.

I make a UITableview that Contain CustomCell. My CustomCell contain UITextView and Some other Controls but I want to set My Cell Height like as 300 if my UITextViewtext Contain Some text and if my TextView text is Contain null text means not any Text then I want my Cell Height 170 how it possible. I write a code for that but it is not working.

我的代码就像

NSString *magazineDescription=[self.firstArray valueForKey:@"long_description"];
if([magazineDescription isKindOfClass:[NSString class]])
{
    cellOne.magazineDescriptionTextView.text = magazineDescription;
    cellOne.frame=CGRectMake(0, 0, 300, 300);
}
else
{
    cellOne.magazineDescriptionTextView.text =  @"";
    cellOne.magazineDescriptionTextView.hidden=TRUE;
    cellOne.frame=CGRectMake(0, 0, 300, 170);
}

这里我的 self.firstArray 包含 JSON 数据值,有时我的 keyValue @"long_description" 为空,所以我想隐藏单元格中的 TextView 和设置单元格的高度从 300 到 170 和宽度都希望在两种情况下保持相同.

Here my self.firstArray Contain JSON data value and sometime my keyValue @"long_description" is null so I want to hide The TextView From Cell and Set height of Cell from 300 to 170 and Width are want to remaining same for both cases.

我知道这样的方法:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

但是在这个方法中如何在这个方法中为这个案例设置高度.请给我解决方案.

But in this method how to set height in this Method for this Case .please give me Solution for that.

推荐答案

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = @"Your text";
    UIFont *cellFont = [UIFont systemFontOfSize:YourFontSize];
    CGSize constraintSize = CGSizeMake(TextviewWidth, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    NSLog(@"labelSize : %f", labelSize.height);
    return labelSize.height;
}

这篇关于如何以编程方式设置 Tableview CustomCell 高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆