iOS 8:使用动态行高和自动布局...但也使所有单元格的高度相同

时间:2014-11-04 00:29:15

标签: uitableview autolayout

我有一个有四行的UITableView。我的四个单元格中都有不同数量的多行内容。当然我喜欢自动布局确定每个单元格的最佳高度......但我希望四个单元格中的每一个都是相同的高度,高度是最高的高度细胞。

这可能吗?

1 个答案:

答案 0 :(得分:0)

可能是的,但您需要进行计算,然后重新加载tableView,而不是让heightForRowAtIndexPath:(NSIndexPath *)indexPath为您处理...

例如

-(CGFloat)findGreatestCellHeight
{
    CGFloat height = 0.0f;

    for (NSInteger i = 0; i < CELL_COUNT; i++) {

        NSString *text = [TEXT_ARRAY objectAtIndex:i];

// note WIDTH should match your cells textLabel width... Personally I'd explicitly set that in a custom UITableViewCell

        CGSize textSize = [text sizeWithFont:THE_TEXTLABELS_FONT constrainedToSize:CGSizeMake(WIDTH, HUGE_VALF) lineBreakMode:NSLineBreakByWordWrapping]; 


        if (textSize.height > height)
            height = textSize.height;
    }

    return height;
}
相关问题