如何计算UITableViewCell高度看内容

时间:2013-07-12 17:56:47

标签: ios objective-c uitableview

我需要确定单元格高度并放置在此单元格文本中,其中包含图像,例如: 那里有一些文字 图片 那里有一些文字 图片 等等。 怎么实现这个?建议请。 日Thnx。 我希望在单个单元格中划分我的段落,如下所示: enter image description here

1 个答案:

答案 0 :(得分:0)

因为内容是动态的,所以一种方法是在被调用的heightForRowAtIndexPath(或等效方法)之前计算单元格的高度,并将这些值存储在数组中。

-(void)calculateCellHeights:(NSArray *)contentArray
{
    self.heightArray = [[NSMutableArray alloc] initWithCapacity: contentArray.count];
    for( MyCellContent *content in contentArray )
    {
        CGFloat totalHeight = 0;
        totalHeight += content.image.size.height;
        CGSize titleSize = [content.title sizeWithFont:self.myTitleFont];
        totalHeight += titleSize.height;
        CGSize textContentSize = [content.textContent sizeWithFont:self.myTextContentFont];
        totalHeight += textContentSize.height;
        [self.heightArray addObject:@(totalHeight)];
    }
}


-(CGFloat) tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*) indexPath
{
    return self.heightArray[[indexPath.row floatValue]];
}

这假设只有一个部分并使用常规文本字符串而不是NSAttributedStrings,并且不处理界面元素之间的填充,但是你得到了图片。

相关问题