[英]How to CORRECTLY increase UITableViewCell using UILabel's height
There have been so many answers to this question on stack overflow, please know that I have looked but all the solutions do not work for me.关于堆栈溢出的这个问题有很多答案,请知道我已经看过但所有的解决方案都不适合我。 I have two UILabels in the cell, one has a definite width and height, the other which will be dynamic, I put the width about <= 250 and the height >=50.
我在单元格中有两个 UILabels,一个有明确的宽度和高度,另一个是动态的,我把宽度设置为 <= 250,高度 >=50。 I have also set all four margins to the content view and then I tried these two methods below to get the height of the string:
我还为内容视图设置了所有四个边距,然后我尝试了下面这两种方法来获取字符串的高度:
var context: NSStringDrawingContext = NSStringDrawingContext()
context.minimumScaleFactor = 0.8
cell?.body.text = msg.body
var width: CGFloat = (cell?.body.frame.size.width)!
width = ((UIScreen.main.bounds.width)/320)*width
let size: CGSize = (cell?.body.text!.boundingRect(with: CGSize(width: width, height: 2000), options:NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName: cell?.body.font], context: context).size)!
print("size.height is \(size.height)")
return size.height
and this method:这个方法:
var optimalHeight : CGFloat
{
get
{
let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: CGFloat.greatestFiniteMagnitude))
//let label = UILabel(frame: CGRectMake(0, 0, self.frame.width, CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = self.lineBreakMode
label.font = self.font
label.text = self.text
label.sizeToFit()
return label.frame.height
}
}
and for these two, the image attached is what shows as the UILabel, which, as anyone can see, is way off.对于这两个,附加的图像显示为 UILabel,正如任何人都看到的那样,它是遥远的。
I have also tried implementing UITableViewAutomaticDimension in the estimatedHeightForRowAt and heightForRowAt methods, but this did not work, after two lines, it would not expand further.我也尝试过在estimatedHeightForRowAt 和heightForRowAt 方法中实现UITableViewAutomaticDimension,但这不起作用,在两行之后,它不会进一步扩展。 I tried calling estimatedRowHeight and rowHeight in the viewDidLoad() method like this:
我尝试在 viewDidLoad() 方法中调用estimatedRowHeight 和rowHeight,如下所示:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300 //even went as far as using 800
same thing, stopped expanding after 2 lines.同样的事情,在 2 行后停止扩展。 I also saw something about implementing these methods:
我还看到了一些关于实现这些方法的内容:
tableView.setNeedsLayout()
tableView.layoutIfNeeded()
and yet nothing!然而什么都没有! I have looked through the raywenderlich tutorial and a lot more.
我已经浏览了 raywenderlich 教程和更多内容。 I have no clue what I am doing wrong.
我不知道我做错了什么。 This might be a duplicate question but I assure you I have tried every solution and nothing works for me.
这可能是一个重复的问题,但我向您保证,我已经尝试了所有解决方案,但没有任何解决方案对我有用。 Please could someone take a look at this and see what I am doing wrong?
请有人看看这个,看看我做错了什么?
these are the screenshots of the constraints for both labels这些是两个标签约束的屏幕截图
So I wrapped my dequeuing in cellForRowAt on a background thread and this prevented my cells from being dynamic.所以我在后台线程上将我的出列包装在 cellForRowAt 中,这阻止了我的单元格动态。 After I took that statement out, it worked perfectly!
在我取出那条语句后,它完美地工作了! Such a waste of code time fixing something that was never broken
浪费代码时间来修复从未被破坏的东西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.