繁体   English   中英

如何计算UITableViewCell高度看到的内容

[英]how calculate UITableViewCell height seeing content

我需要确定单元格的高度和位置,在该单元格中包含图像的文本中,例如:一些文本在那里image sometext那里image ... ect。 如何实现呢? 建议请。 谢谢 我想将段落分为以下单个单元格: 在此处输入图片说明

因为内容是动态的,所以一种方法是在被调用的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,并且不处理界面元素之间的填充,但是您会得到图片。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM