自定义UITableViewCell中的UILabel无法以正确的尺寸绘制 [英] UILabel inside custom UITableViewCell not drawing at the correct size

查看:46
本文介绍了自定义UITableViewCell中的UILabel无法以正确的尺寸绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义表格单元,其中包含许多UILabel.在运行时,我正在使用sizeWithFont:constrainedToSize:lineBreakMode:调整标签的高度以适合其内容,并相应地重新放置它们.单元格中的最后一个标签包含大量文本,导致其换行,而我遇到了一个非常奇怪的问题.尽管sizeWithFont调用返回正确的大小,并且我将UILabel的框架设置为该高度,但它绘制了几行短的线.此屏幕快照说明了我在说什么:

I have a custom table cell which contains a number of UILabels. At runtime, I am adjusting the height of the labels to fit their contents using sizeWithFont:constrainedToSize:lineBreakMode: and repositioning them accordingly. The last label in the cell contains a large amount of text, causing it to wrap, and I'm having a very odd problem. Although the sizeWithFont call returns the correct size, and I'm setting the UILabel's frame to that height, it draws a couple of lines short. This screenshot illustrates what I'm talking about:

在此示例中,完整文本块的高度应为90(在Interface Builder中检查),这就是从sizeWithFont返回的结果.这也是UILabel框架设置的高度,我已经通过记录以及停止执行和检查该值来进行验证.但是,正如您所看到的,虽然正确地为其分配了空间(显然,"<已编辑"上方的黑色细线是表格单元格边框),但并非绘制整个90像素高.我很困惑.如果有人能提供一些有关为何采用这种方式的见解,我将不胜感激.

In this example, the height of the full block of text should be 90 (as checked in Interface Builder), and that's what returns from sizeWithFont. It's also the height that the UILabel's frame is set to, which I have verified by logging and also by stopping execution and inspecting the value. However, as you can see, it's clearly not drawing the full 90 pixels high, although it's correctly allocating the space for it (the thin black line above 'Edited' is the table cell border). I'm completely perplexed. If anyone can offer some insight as to why it's behaving this way, I would be very grateful.

推荐答案

最后,是一个解决方案!

At last, a solution!

事实证明,该单元确实进行了两次布局-一次是在heightForRowAtIndexPath期间,这是我调整子视图和该单元的所有高度的位置,后来是在源自__CFRunLoopDoObservers的某些不可跟踪的事务中进行的.我是怎么追踪到的?我在我的自定义表格视图单元格类中添加了一个layoutSubviews覆盖,以便可以对其断点.

Turns out that the cell does layout twice -- once during heightForRowAtIndexPath, which is where I tweak all the heights of the subviews and the cell, and later during some untraceable transaction originating in __CFRunLoopDoObservers. How did I trace this? I added a layoutSubviews override to my custom table view cell class so I could breakpoint it.

在第二遍过程中,最后的UILabel子视图被设置为比我设置的高度更短的高度,这可能是根据某些奥秘的自动调整大小规则而定的. (是的,我尝试先对所有这些设置进行调整,但均未成功.)事实证明,仅在layoutSubviews中不执行任何操作都会禁用此框架行为,从而使我可以完全控制视图的绘制方式.

During the second pass, the last UILabel subview was getting set to a shorter height than I set it to, probably in accordance with some arcane autoresizing rules. (Yes, I tried tweaking all of those settings first, with no success.) As it turns out, merely doing nothing in layoutSubviews disabled this framework behavior, allowing me to completely control how my views draw.

这篇关于自定义UITableViewCell中的UILabel无法以正确的尺寸绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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