如何设置Tableview的CustumCell高度编程?

我是iOS开发新手。 我做一个UITableview包含CustumCell.My CustumCell包含UITextView和一些其他控件但是我想设置我的单元格高度像300如果我的UITextViewtext包含一些文本,如果我的TextView文本是包含空文本意味着没有任何文本,那么我想我单元格高度170怎么可能。 我写了一个代码,但它不工作。

我的代码就像

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和宽度都希望保持相同的两种情况下。

我知道像这样的方法

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

但是在这种方法中,如何设置此方法的高度,请给我解决scheme。

 - (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; } 
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { //Fetch your string from array as below or according to your code NSString *magazineDescription=[self.firstArray valueForKey:@"long_description"]; if(magazineDescription!=nil) { return 300.0f; } else { return 170.0f; } } 

首先,向客户申报CustomerCell

 @property (strong, nonatomic) CustomerCell *cell; 

在tableViewDelegate中

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if ([@"" isEqualToString:_cell.textView.text] || _cell.textView.text == nil) { return 170; } else { return 300; } } 

希望它的帮助