TableView动态行高不被调用

时间:2013-09-07 13:11:25

标签: ios objective-c uitableview

我有一个应用程序,它在顶部显示一个TableView,其中有两个相对静态的单元格,后面是一系列包含标签和分段控件的自定义单元格。这些单元格需要根据标签中的文本数量而变化。

我正在计算cellForRowAtIndexPath中所需的单元格高度,将值存储在数组中,然后在heightForRowAtIndexPath中使用该数组中的值。但是,似乎首先调用了heightForRowAtIndexPath,因此我的所有行高都是0 / nil。

如果在配置单元格之前需要知道单元格高度,如何根据单元格的特定内容指定行高?

来自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;
}

来自heightForRowAtIndexPath的代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger currentIndex = indexPath.item;
    return [[self.cellHeightList objectAtIndex:currentIndex] integerValue];
}

谢谢!

3 个答案:

答案 0 :(得分:2)

首先确保不要在indexPath上使用'item',而是使用'row'。我认为使用的是UICollectionView,而不是UITableView。 其次,你需要在没有手头的细胞的情况下计算细胞的大小。您的tableView具有一定的宽度,因此基于此,您可以计算所需的高度。将此计算放在heightForRowAtIndexPath中:(如果这是密集的,请将其缓存)。

具体来说,将以下代码放在cellForRowAtIndexPath中但不在heightForRowAtIndexPath中:

NSInteger labelWidth = [UIScreen mainScreen].applicationFrame.size.width - 140;
CGSize maxSize = CGSizeMake(labelWidth, MAXFLOAT);
CGSize labelSize;

此外,不使用UIScreen,只需使用tableView的宽度,它就会使你的代码紧密耦合。

答案 1 :(得分:1)

You can use autolayout and set the height based on the content in the content view 
you need to set 

//swift

     override func viewDidLoad() 
        {
             self.tableView.estimatedRowHeight = 180  
        }

    // return  UITableViewAutomaticDimension 
      override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
        {
            return UITableViewAutomaticDimension
        }



//Objective C

        - (void)viewDidLoad
        {
            [super viewDidLoad];
            self.tableview.estimatedRowHeight = 180.0 ;

        }

        -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //constraint for autolayout for ios8+
        return UITableViewAutomaticDimension;
    }

答案 2 :(得分:0)

我使用Joride的答案作为一个很好的起点,然后将标签高度功能拉出到一个单独的功能中。我想我会抽象出更多的缺口,只是从该函数返回标签的实际CGSize,这样我就可以从heightForRowAtIndexPath和cellForRowAtIndexPath中干净地调用它。我在下面提供了我的更新代码。

的cellForRowAtIndexPath:

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

heightForRowAtIndexPath:

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

}