2010-09-17 63 views

回答

51

你可以看一下这个方法:

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

在你的情况下,代码应该是这样的:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 1 && indexPath.row == 1) { 
     return SPECIAL_HEIGHT; 
    } 
    return NORMAL_HEIGHT; 
} 

你可以看看了解更多详细the method here

+0

好的,谢谢!但我有一个问题...如果我输入一个像返回60.0的数字;工作很大,但如果我输入一些代码来返回基于UILabel文本的高度,单元格背景不会出现......我在cellForRowAtIndexPath方法中添加了这个代码: – matteodv 2010-09-17 17:04:33

+0

else if(indexPath.section == 1 && indexPath.row == 1){ \t \t descrizioneLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,5,285,50)]; \t \t descrizioneLabel.font = [UIFont boldSystemFontOfSize:14.0]; \t \t descrizioneLabel。backgroundColor = [UIColor clearColor]; \t \t descrizioneLabel.numberOfLines = 0; \t \t descrizioneLabel.text = wish.descrizione; \t \t [cell.contentView addSubview:descrizioneLabel]; \t} – matteodv 2010-09-17 17:04:54

+0

这是您发布的方法: - (CGFloat的)的tableView:(UITableView的*)的tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { \t \t如果(indexPath.section == 1 && indexPath.row == 1){ \t \t NSString * test = descrizioneLabel.text; \t \t UIFont * cellFont = [UIFont boldSystemFontOfSize:14.0]; \t \t CGSize constraintSize = CGSizeMake(280.0,MAXFLOAT); \t \t CGSize labelSize = [testo sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; \t \t \t \t return labelSize.height; \t} \t \t return 44.0; } – matteodv 2010-09-17 17:05:39

0

看一看

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

对于indexpath只是检查该行部分和调节高度相应

3

的iOS 8以上我们可以使用动态表格视图单元格高度。

使用此功能UITableviewCell从其内容获取其高度和我们不需要写heightForRowAtIndexPath

所有我在viewDidLoad中()

tableView.estimatedRowHeight = 44.0; 
tableView.rowHeight = UITableViewAutomaticDimension; 

做如果cell包含uilabel。然后应用约束uilabel

enter image description here

确保您更改标签--Lines-- 0

enter image description here

细胞会随着uilabel内容自动增长:

enter image description here

相关问题