ios – 基于UILabel文本量的UITableViewCell自动高度

栏目: IOS · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/42717173/uitableviewcell-auto-height-based-on-amount-of-uilabel-text

我的故事板中有一个棘手的设置,我有一个UIViewController,它包含一个UITableViewController.在UITableViewController中,我有几个原型单元,我已经链接到代码中的子类uitableviewcell对象.

使用约束和故事板我想根据UILabel最终的大小来改变原型单元格的高度,这取决于进入它的文本.

目前我有一个

UIViewController
-- UITableViewController
-- -- UITableViewCell ((melchat) prototype cell)
-- -- -- ContentView
-- -- -- -- UIView ((background) view with drop shadow card type effect)
-- -- -- -- -- UIImageView (Avatar)
-- -- -- -- -- IUlabel (dynamic (depending on code/input) multi line UILabel)

有些我希望UILabel调整UIView(背景)的大小然后反过来影响UITableViewCell的高度.

我正在使用XCode 8.2.1

我在故事板中采用了布局的屏幕截图并应用了约束.

ios – 基于UILabel文本量的UITableViewCell自动高度

更新

我已经更新了我的约束,几乎所有回到ContentView并将uilabel行数更新为0,然后还实现了UITableViewAutomaticDimension代码,但它仍然无效.请参阅下面的代码和屏幕截图.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;

}

ios – 基于UILabel文本量的UITableViewCell自动高度

ios – 基于UILabel文本量的UITableViewCell自动高度

再谈Dao的答案..

他是对的,你需要使用UITableViewAutomaticDimension

此外,您需要确保以一种方式设置约束,即单元格中的所有内容都被约束到单元格contentView.所以你的标签可能需要诸如此类的限制

>对ImageView的主要约束

>对contentView的最高约束

>对contentView的底部约束

>对contentView的尾随约束

确保将UILabel设置为多行(或行= 0),它应该可以工作.

如果您正在使用heightForRowAt委托函数,请确保返回UITableViewAutomaticDimension

翻译自:https://stackoverflow.com/questions/42717173/uitableviewcell-auto-height-based-on-amount-of-uilabel-text


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

You Can Program in C++

You Can Program in C++

Francis Glassborow / John Wiley & Sons / 2006-7 / 406.80元

An interactive and fun way to learn C++, one of the most popular high-level programming languages for graphic applications This unique, hands-on approach to learning C++ makes t......一起来看看 《You Can Program in C++》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试