iOS:自定义表格视图单元格中的约束

Posted

技术标签:

【中文标题】iOS:自定义表格视图单元格中的约束【英文标题】:iOS: Constraint in a custom tableview cell 【发布时间】:2014-04-16 11:31:59 【问题描述】:

我有一个带有自定义单元格的表格视图,其中我有一个文本视图。 对于这个单元格,我用文本的大小确定它的高度,没关系。

我在自动布局中工作,所以对于我的单元格,我使用两个约束以这种方式扩展所有空间单元格的文本视图

我可以说它工作正常,并且我的 textview 会针对所有单元格空间自行扩展。

在视图控制器中,我有一个控件来检查文本(字符串)何时为空。当它为空时,我为单元格高度返回 0。

但是在这种情况下,当我返回 0 并且 text_view 为空时,我发生了崩溃,但我不知道如何解决。

这是消息

Unable to simultaneously satisfy constraints.

Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x1704864a0 V:|-(25)-[UITextView:0x15454d960]   (Names: '|':UITableViewCellContentView:0x178179680 )>",
    "<NSLayoutConstraint:0x1704864f0 V:[UITextView:0x15454d960]-(23)-|   (Names: '|':UITableViewCellContentView:0x178179680 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x17889a220 h=--& v=--& V:[UITableViewCellContentView:0x178179680(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1704864f0 V:[UITextView:0x15454d960]-(23)-|   (Names: '|':UITableViewCellContentView:0x178179680 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

【问题讨论】:

如果没有内容,为什么要包括该行? 尝试在自定义单元格中设置[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO]; 将视图的 alpha 设置为 0 是隐藏视图的更好方法。 我不改变行数,因为我经常有 7 行,完全不同。而且很容易隐藏没有数据的行,但现在我用 '[cell.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];' 解决了感谢@Akhilrajtr 【参考方案1】:

将单元格内容的TranslatesAutoresizingMaskIntoConstraints设置为NO查看方式,

[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];

【讨论】:

以上是关于iOS:自定义表格视图单元格中的约束的主要内容,如果未能解决你的问题,请参考以下文章

IOS/Objective-C:检测自定义表格视图单元格中的按钮按下?

修复表格视图单元格中的文本,ios?

自定义表格视图单元格中的 JSON 缩略图图像

Obj-C - 点击自定义表格视图单元格中的删除表格视图行按钮

使用自定义表格视图单元格中的故事板segue

自定义表格视图单元格中的可编辑文本字段