问题描述
我的故事板中有一些棘手的设置,我有一个UIViewController,它包含一个UITableViewController。在UITableViewController中,我有几个原型单元,我已经链接到代码中的子类uitableviewcell对象。
I have a bit of a tricky set up in my storyboard, I have a UIViewController that holds a UITableViewController. Within the UITableViewController I have several prototypecells I have linked to subclassed uitableviewcell objects in code.
使用约束和故事板我想改变原型单元格的高度取决于UILabel的大小最终取决于进入它的文本。
Using constraints and storyboard I would like to change the height of my prototype cell depending on the size the UILabel ends up being which is dependant on the text going into it.
目前我有一个
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的高度。
Some how I would like the UILabel to resize the UIView (background) then in turn effect the height of that UITableViewCell.
我正在使用XCode 8.2.1
I am using XCode 8.2.1
我在故事板和构造中拍摄了布局的屏幕截图aints applied。
I have taken a screen shot of the layout in storyboard and constraints applied.
我已经更新了我的约束,几乎所有回到ContentView并将uilabel行数更新为0,然后还实现了UITableViewAutomaticDimension代码,但它仍然无效。请参阅下面的代码和屏幕截图。
I have updated my constraints to pretty much all go back to ContentView and have updated uilabel line count to 0 and then also implemented the UITableViewAutomaticDimension code but its still not working. Please see code and screen shots below.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
推荐答案
要更进一步了解Dao的回答..
To go a little further on Dao's answer..
他是对的,你需要使用 UITableViewAutomaticDimension
He is correct, you need to use UITableViewAutomaticDimension
此外,您需要确保以一种方式设置约束,即单元格中的所有内容都被约束到单元格contentView。因此,您的标签可能需要约束,例如
Also, you need to ensure that you setup your constraints in a way that all of the content in the cell is constrained to the cells contentView. So your label will likely need constraints such as
- 对ImageView的主要约束
- 对contentView的最高约束
- 对contentView的底部约束
- 对contentView的尾随约束
- Leading constraint to ImageView
- Top constraint to contentView
- Bottom constraint to contentView
- Trailing constraint to contentView
确保将UILabel设置为多行(或行= 0),它应该可以工作。
Make sure that you set the UILabel to multiline (or lines = 0) and it should work.
如果您使用的是heightForRowAt委托函数,请确保返回 UITableViewAutomaticDimension
If you are using the heightForRowAt delegate functions ensure you return UITableViewAutomaticDimension
这篇关于UITableViewCell自动高度基于UILabel文本的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!