Ios 未调用TableView动态行高度
我有一个应用程序,它显示一个TableView,顶部有两个相对静态的单元格,后面是一系列包含标签和分段控件的自定义单元格。这些单元格的高度需要根据标签中的文本量而变化 我正在计算cellForRowAtIndexPath中所需的单元格高度,将值存储在数组中,然后在heightForRowAtIndexPath中使用该数组中的值。然而,heightForRowAtIndexPath似乎是首先被调用的,因此我的所有行高度都是0/nil 在配置单元格之前需要知道单元格高度时,如何根据单元格的特定内容指定行高度 cellForRowAtIndexPath中的代码段:Ios 未调用TableView动态行高度,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个应用程序,它显示一个TableView,顶部有两个相对静态的单元格,后面是一系列包含标签和分段控件的自定义单元格。这些单元格的高度需要根据标签中的文本量而变化 我正在计算cellForRowAtIndexPath中所需的单元格高度,将值存储在数组中,然后在heightForRowAtIndexPath中使用该数组中的值。然而,heightForRowAtIndexPath似乎是首先被调用的,因此我的所有行高度都是0/nil 在配置单元格之前需要知道单元格高度时,如何根据单元格的特定内容指
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.item;
if (indexPath.item == 0){
static NSString *CellIdentifier = @"MeasurementCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self.cellHeightList insertObject:[NSNumber numberWithInt:44] atIndex:currentIndex];
return cell;
} else if (indexPath.item == 1){
if (self.dataController.isScoreAvailable){
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self.cellHeightList insertObject:[NSNumber numberWithInt:46] atIndex:currentIndex];
return cell;
} else {
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
[self.cellHeightList insertObject:[NSNumber numberWithInt:0] atIndex:currentIndex];
return cell;
}
} else if (indexPath.item > 1){
NSInteger labelWidth = [UIScreen mainScreen].applicationFrame.size.width - 140; //80 for segment + 3*20 for margins & spacing
CGSize maxSize = CGSizeMake(labelWidth, MAXFLOAT);
CGSize labelSize;
static NSString *CellIdentifier = @"QuestionCell";
InterviewQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
InterviewQuestion *questionAtIndex = [self.dataController objectInListAtIndex:(indexPath.item-2)];
cell.questionLabel.text = questionAtIndex.questionText;
labelSize = [cell.questionLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect labelFrame = CGRectMake(0, 0, labelWidth, labelSize.height);
cell.questionLabel.frame = labelFrame;
cell.questionLabel.numberOfLines = 0;
cell.answerControl.selectedSegmentIndex = questionAtIndex.answer;
cell.answerControl.tag = indexPath.item;
[self.cellHeightList insertObject:[NSNumber numberWithInt:labelSize.height] atIndex:currentIndex];
return cell;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.item;
return [[self.cellHeightList objectAtIndex:currentIndex] integerValue];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.row;
if (currentIndex == 0){
static NSString *CellIdentifier = @"MeasurementCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell textLabel] setText:@"Eating"];
[self.cellHeightList insertObject:[NSNumber numberWithInt:44] atIndex:currentIndex];
return cell;
} else if (currentIndex == 1){
if (self.dataController.isScoreAvailable){
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell textLabel] setText:@"Score"];
[[cell detailTextLabel] setText:@"Score short description"];
[self.cellHeightList insertObject:[NSNumber numberWithInt:46] atIndex:currentIndex];
return cell;
} else {
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell textLabel] setText:@""];
[[cell detailTextLabel] setText:@""];
cell.accessoryType = UITableViewCellAccessoryNone;
[self.cellHeightList insertObject:[NSNumber numberWithInt:0] atIndex:currentIndex];
return cell;
}
} else if (currentIndex > 1){
NSInteger labelWidth = [UIScreen mainScreen].applicationFrame.size.width - 140;
CGSize maxSize = CGSizeMake(labelWidth, MAXFLOAT);
static NSString *CellIdentifier = @"QuestionCell";
InterviewQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
InterviewQuestion *questionAtIndex = [self.dataController objectInListAtIndex:(currentIndex-2)];
cell.questionLabel.text = questionAtIndex.questionText;
CGSize labelSize = [cell.questionLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect labelFrame = CGRectMake(0, 0, labelWidth, labelSize.height);
cell.questionLabel.frame = labelFrame;
cell.questionLabel.numberOfLines = 0;
cell.answerControl.selectedSegmentIndex = questionAtIndex.answer;
cell.answerControl.tag = indexPath.item;
[self.cellHeightList insertObject:[NSNumber numberWithInt:labelSize.height] atIndex:currentIndex];
return cell;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.row;
if (currentIndex == 0){
return 44;
} else if (currentIndex == 1){
if (self.dataController.isScoreAvailable){
return 46;
} else {
return 0;
}
} else if (currentIndex > 1){
return [self getRowHeightForRow:currentIndex];
}
return 0;
}
heightForRowAtIndexPath中的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.item;
if (indexPath.item == 0){
static NSString *CellIdentifier = @"MeasurementCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self.cellHeightList insertObject:[NSNumber numberWithInt:44] atIndex:currentIndex];
return cell;
} else if (indexPath.item == 1){
if (self.dataController.isScoreAvailable){
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self.cellHeightList insertObject:[NSNumber numberWithInt:46] atIndex:currentIndex];
return cell;
} else {
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
[self.cellHeightList insertObject:[NSNumber numberWithInt:0] atIndex:currentIndex];
return cell;
}
} else if (indexPath.item > 1){
NSInteger labelWidth = [UIScreen mainScreen].applicationFrame.size.width - 140; //80 for segment + 3*20 for margins & spacing
CGSize maxSize = CGSizeMake(labelWidth, MAXFLOAT);
CGSize labelSize;
static NSString *CellIdentifier = @"QuestionCell";
InterviewQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
InterviewQuestion *questionAtIndex = [self.dataController objectInListAtIndex:(indexPath.item-2)];
cell.questionLabel.text = questionAtIndex.questionText;
labelSize = [cell.questionLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect labelFrame = CGRectMake(0, 0, labelWidth, labelSize.height);
cell.questionLabel.frame = labelFrame;
cell.questionLabel.numberOfLines = 0;
cell.answerControl.selectedSegmentIndex = questionAtIndex.answer;
cell.answerControl.tag = indexPath.item;
[self.cellHeightList insertObject:[NSNumber numberWithInt:labelSize.height] atIndex:currentIndex];
return cell;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.item;
return [[self.cellHeightList objectAtIndex:currentIndex] integerValue];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.row;
if (currentIndex == 0){
static NSString *CellIdentifier = @"MeasurementCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell textLabel] setText:@"Eating"];
[self.cellHeightList insertObject:[NSNumber numberWithInt:44] atIndex:currentIndex];
return cell;
} else if (currentIndex == 1){
if (self.dataController.isScoreAvailable){
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell textLabel] setText:@"Score"];
[[cell detailTextLabel] setText:@"Score short description"];
[self.cellHeightList insertObject:[NSNumber numberWithInt:46] atIndex:currentIndex];
return cell;
} else {
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell textLabel] setText:@""];
[[cell detailTextLabel] setText:@""];
cell.accessoryType = UITableViewCellAccessoryNone;
[self.cellHeightList insertObject:[NSNumber numberWithInt:0] atIndex:currentIndex];
return cell;
}
} else if (currentIndex > 1){
NSInteger labelWidth = [UIScreen mainScreen].applicationFrame.size.width - 140;
CGSize maxSize = CGSizeMake(labelWidth, MAXFLOAT);
static NSString *CellIdentifier = @"QuestionCell";
InterviewQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
InterviewQuestion *questionAtIndex = [self.dataController objectInListAtIndex:(currentIndex-2)];
cell.questionLabel.text = questionAtIndex.questionText;
CGSize labelSize = [cell.questionLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect labelFrame = CGRectMake(0, 0, labelWidth, labelSize.height);
cell.questionLabel.frame = labelFrame;
cell.questionLabel.numberOfLines = 0;
cell.answerControl.selectedSegmentIndex = questionAtIndex.answer;
cell.answerControl.tag = indexPath.item;
[self.cellHeightList insertObject:[NSNumber numberWithInt:labelSize.height] atIndex:currentIndex];
return cell;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.row;
if (currentIndex == 0){
return 44;
} else if (currentIndex == 1){
if (self.dataController.isScoreAvailable){
return 46;
} else {
return 0;
}
} else if (currentIndex > 1){
return [self getRowHeightForRow:currentIndex];
}
return 0;
}
谢谢大家! 首先确保在indexPath上不使用'item',而是使用'row'。我认为UICollectionView使用的是项,而不是UITableView。 第二,您需要某种方法来计算单元格的大小,而不需要将单元格放在手边。您的tableView具有一定的宽度,因此基于此,您可以计算必要的高度。将此计算放在HeightForRowatineXpath中:(如果这是密集型的,请缓存它) 具体而言,将以下代码不放置在CellForRowatineXpath中,而是放置在HeightForRowatineXpath中:
NSInteger labelWidth = [UIScreen mainScreen].applicationFrame.size.width - 140;
CGSize maxSize = CGSizeMake(labelWidth, MAXFLOAT);
CGSize labelSize;
另外,不使用UI屏幕,只需使用tableView的宽度,它会降低代码之间的紧密耦合。我使用Joride的答案作为一个很好的起点,然后将标签高度功能拉到一个单独的函数中。我想我会再提取一个缺口,然后从该函数返回标签的实际CGSize,这样我就可以从HeightForRowatineXpath和CellForRowatineXpath中清晰地调用它。我在下面包含了我的更新代码 cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.item;
if (indexPath.item == 0){
static NSString *CellIdentifier = @"MeasurementCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self.cellHeightList insertObject:[NSNumber numberWithInt:44] atIndex:currentIndex];
return cell;
} else if (indexPath.item == 1){
if (self.dataController.isScoreAvailable){
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self.cellHeightList insertObject:[NSNumber numberWithInt:46] atIndex:currentIndex];
return cell;
} else {
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
[self.cellHeightList insertObject:[NSNumber numberWithInt:0] atIndex:currentIndex];
return cell;
}
} else if (indexPath.item > 1){
NSInteger labelWidth = [UIScreen mainScreen].applicationFrame.size.width - 140; //80 for segment + 3*20 for margins & spacing
CGSize maxSize = CGSizeMake(labelWidth, MAXFLOAT);
CGSize labelSize;
static NSString *CellIdentifier = @"QuestionCell";
InterviewQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
InterviewQuestion *questionAtIndex = [self.dataController objectInListAtIndex:(indexPath.item-2)];
cell.questionLabel.text = questionAtIndex.questionText;
labelSize = [cell.questionLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect labelFrame = CGRectMake(0, 0, labelWidth, labelSize.height);
cell.questionLabel.frame = labelFrame;
cell.questionLabel.numberOfLines = 0;
cell.answerControl.selectedSegmentIndex = questionAtIndex.answer;
cell.answerControl.tag = indexPath.item;
[self.cellHeightList insertObject:[NSNumber numberWithInt:labelSize.height] atIndex:currentIndex];
return cell;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.item;
return [[self.cellHeightList objectAtIndex:currentIndex] integerValue];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.row;
if (currentIndex == 0){
static NSString *CellIdentifier = @"MeasurementCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell textLabel] setText:@"Eating"];
[self.cellHeightList insertObject:[NSNumber numberWithInt:44] atIndex:currentIndex];
return cell;
} else if (currentIndex == 1){
if (self.dataController.isScoreAvailable){
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell textLabel] setText:@"Score"];
[[cell detailTextLabel] setText:@"Score short description"];
[self.cellHeightList insertObject:[NSNumber numberWithInt:46] atIndex:currentIndex];
return cell;
} else {
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell textLabel] setText:@""];
[[cell detailTextLabel] setText:@""];
cell.accessoryType = UITableViewCellAccessoryNone;
[self.cellHeightList insertObject:[NSNumber numberWithInt:0] atIndex:currentIndex];
return cell;
}
} else if (currentIndex > 1){
NSInteger labelWidth = [UIScreen mainScreen].applicationFrame.size.width - 140;
CGSize maxSize = CGSizeMake(labelWidth, MAXFLOAT);
static NSString *CellIdentifier = @"QuestionCell";
InterviewQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
InterviewQuestion *questionAtIndex = [self.dataController objectInListAtIndex:(currentIndex-2)];
cell.questionLabel.text = questionAtIndex.questionText;
CGSize labelSize = [cell.questionLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect labelFrame = CGRectMake(0, 0, labelWidth, labelSize.height);
cell.questionLabel.frame = labelFrame;
cell.questionLabel.numberOfLines = 0;
cell.answerControl.selectedSegmentIndex = questionAtIndex.answer;
cell.answerControl.tag = indexPath.item;
[self.cellHeightList insertObject:[NSNumber numberWithInt:labelSize.height] atIndex:currentIndex];
return cell;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.row;
if (currentIndex == 0){
return 44;
} else if (currentIndex == 1){
if (self.dataController.isScoreAvailable){
return 46;
} else {
return 0;
}
} else if (currentIndex > 1){
return [self getRowHeightForRow:currentIndex];
}
return 0;
}
ROWATINDEXPATH的高度:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.item;
if (indexPath.item == 0){
static NSString *CellIdentifier = @"MeasurementCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self.cellHeightList insertObject:[NSNumber numberWithInt:44] atIndex:currentIndex];
return cell;
} else if (indexPath.item == 1){
if (self.dataController.isScoreAvailable){
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self.cellHeightList insertObject:[NSNumber numberWithInt:46] atIndex:currentIndex];
return cell;
} else {
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
[self.cellHeightList insertObject:[NSNumber numberWithInt:0] atIndex:currentIndex];
return cell;
}
} else if (indexPath.item > 1){
NSInteger labelWidth = [UIScreen mainScreen].applicationFrame.size.width - 140; //80 for segment + 3*20 for margins & spacing
CGSize maxSize = CGSizeMake(labelWidth, MAXFLOAT);
CGSize labelSize;
static NSString *CellIdentifier = @"QuestionCell";
InterviewQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
InterviewQuestion *questionAtIndex = [self.dataController objectInListAtIndex:(indexPath.item-2)];
cell.questionLabel.text = questionAtIndex.questionText;
labelSize = [cell.questionLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect labelFrame = CGRectMake(0, 0, labelWidth, labelSize.height);
cell.questionLabel.frame = labelFrame;
cell.questionLabel.numberOfLines = 0;
cell.answerControl.selectedSegmentIndex = questionAtIndex.answer;
cell.answerControl.tag = indexPath.item;
[self.cellHeightList insertObject:[NSNumber numberWithInt:labelSize.height] atIndex:currentIndex];
return cell;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.item;
return [[self.cellHeightList objectAtIndex:currentIndex] integerValue];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.row;
if (currentIndex == 0){
static NSString *CellIdentifier = @"MeasurementCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell textLabel] setText:@"Eating"];
[self.cellHeightList insertObject:[NSNumber numberWithInt:44] atIndex:currentIndex];
return cell;
} else if (currentIndex == 1){
if (self.dataController.isScoreAvailable){
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell textLabel] setText:@"Score"];
[[cell detailTextLabel] setText:@"Score short description"];
[self.cellHeightList insertObject:[NSNumber numberWithInt:46] atIndex:currentIndex];
return cell;
} else {
static NSString *CellIdentifier = @"ScoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell textLabel] setText:@""];
[[cell detailTextLabel] setText:@""];
cell.accessoryType = UITableViewCellAccessoryNone;
[self.cellHeightList insertObject:[NSNumber numberWithInt:0] atIndex:currentIndex];
return cell;
}
} else if (currentIndex > 1){
NSInteger labelWidth = [UIScreen mainScreen].applicationFrame.size.width - 140;
CGSize maxSize = CGSizeMake(labelWidth, MAXFLOAT);
static NSString *CellIdentifier = @"QuestionCell";
InterviewQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
InterviewQuestion *questionAtIndex = [self.dataController objectInListAtIndex:(currentIndex-2)];
cell.questionLabel.text = questionAtIndex.questionText;
CGSize labelSize = [cell.questionLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect labelFrame = CGRectMake(0, 0, labelWidth, labelSize.height);
cell.questionLabel.frame = labelFrame;
cell.questionLabel.numberOfLines = 0;
cell.answerControl.selectedSegmentIndex = questionAtIndex.answer;
cell.answerControl.tag = indexPath.item;
[self.cellHeightList insertObject:[NSNumber numberWithInt:labelSize.height] atIndex:currentIndex];
return cell;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger currentIndex = indexPath.row;
if (currentIndex == 0){
return 44;
} else if (currentIndex == 1){
if (self.dataController.isScoreAvailable){
return 46;
} else {
return 0;
}
} else if (currentIndex > 1){
return [self getRowHeightForRow:currentIndex];
}
return 0;
}
新的getRowHeightForRow函数:
- (NSInteger)getRowHeightForRow:(NSInteger)currentRow{
NSInteger labelWidth = [UIScreen mainScreen].applicationFrame.size.width - 140; //80 for segment + 3*20 for margins & spacing
CGSize maxSize = CGSizeMake(labelWidth, MAXFLOAT);
InterviewQuestion *questionAtIndex = [self.dataController objectInListAtIndex:(currentRow-2)];
NSString *questionText = questionAtIndex.questionText;
CGSize labelSize = [questionText sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height;
}
谢谢你关于“行”而不是“项”的提示。我想那有点干净。至于在heightForRowAtIndexPath中添加调整大小的代码,我需要从我的面试问题中提取文本才能做到这一点。在这两个位置调用InterviewQuestion对象以获取文本是否更干净,还是我应该拉一次并将其存储在此ViewController的属性中?建议的方法是将这些文本项存储在属性(NSArray)上。这样,您可以从两个位置访问项目,而无需额外工作。此外,当该数组中的项目发生更改时,您只需调用-reloadCellAtIndexPath:即可重新加载该单元格。(或者你可以使用丑陋的大锤子:-在开发过程中重新加载数据,看看你的逻辑是否正确。)看起来不错。为了使您的代码更具可读性,我建议您对NSNumbers使用现代Objective-C:而不是。“[NSNumber numberWithInt:46]”,您可以使用“@(46)”(不带引号)。编译器将从中创建一个NSNumber,您的代码基本相同,但可读性更好。实际上,不需要调用
heightforrowatinexpath
。只能在viewDidLoad(或ViewWillDisplay)中使用:tableView.rowHeight=UITableViewAutomaticDimension