0

我需要确定单元格高度并在这个单元格文本中放置图像,例如:一些文本有图像 sometext 有图像......等等。如何实施?建议请。谢谢。我想把我的段落分成单独的单元格,如下所示: 在此处输入图像描述

4

1 回答 1

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,并且不处理界面元素之间的填充,但您得到了图片。

于 2013-07-12T18:31:40.653 回答