将UITableViewCell的大小动态调整为UILabel的高度 [英] Resize UITableViewCell to UILabel's height dynamically

查看:89
本文介绍了将UITableViewCell的大小动态调整为UILabel的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据标签的高度和标签的高度调整单元格的高度。或者有什么方法可以根据在 UITextView 中输入的文字来调整单元格的高度?

I want to resize cell's height according to the label's height and label's height according to text. Or is there any way I can resize the cell's height according to the text entered in UITextView?

推荐答案

此方法自iOS 7.0开始被弃用。

有一个 UITableView 在创建单元格或表之前调用的名为 heightForRowAtIndexPath 的委托方法。

There is a UITableView delegate method called heightForRowAtIndexPath that is called before you create a cell or a table.

您可以使用传递给它的 NSIndexPath 来获取特定行的文本并使用 sizeWithFont 来自 UIStringDrawing.h 的方法,为该行计算 CGSize

You could use the NSIndexPath passed to it to get the text at a specific row and use the sizeWithFont method from UIStringDrawing.h to compute a CGSize for that row.

例如:

CGSize size = [text sizeWithFont:font
                   constrainedToSize:maximumLabelSize
                   lineBreakMode:UILineBreakModeWordWrap];

最后你会返回 size.height

这篇关于将UITableViewCell的大小动态调整为UILabel的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆