隐藏包含 UITextview 的 UITableviewcell(自动布局)
Hide UITableviewcell which contains UITextview (Autolayout)
我在包含 UITextView 的表格视图中有一个单元格。 UITextview 对 uitableviewcell 内容视图有顶部、前导、尾部和底部的约束。如果文本视图包含空文本,我想隐藏 uitableviewcell。为此,我将单元格高度降低为 0。由于文本视图具有关于 UITableViewCell 的约束集。
UITableViewCell
---------------------------
| -T- |
| -L- UITextView -R- |
|_________-B-_____________|
L、T、B、R - 左、上、下、右约束
我遇到了约束问题。
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.
(
"<NSLayoutConstraint:0x7fe5d58753a0 UITableViewCellContentView:0x7fe5d5874fe0.bottomMargin == UITextView:0x7fe5d399c000.bottom>",
"<NSLayoutConstraint:0x7fe5d58753f0 UITableViewCellContentView:0x7fe5d5874fe0.topMargin == UITextView:0x7fe5d399c000.top>",
"<NSLayoutConstraint:0x7fe5d5888a20 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fe5d5874fe0(0.5)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fe5d58753a0 UITableViewCellContentView:0x7fe5d5874fe0.bottomMargin == UITextView:0x7fe5d399c000.bottom>
如何隐藏单元格而不影响自动布局。
您可以将字符串的值保存在全局变量中,然后在 cellForRowAtIndexPath 中检查 var 是否为空,如果不是,您可以 return 单元格。
像这样:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
if (your global var != nil) {
let cell: MyCell? = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as? MyCell
cell?.label.text = your value
return cell!
}
}
我会使用 active
属性 关闭和打开约束。这是最简单的方法,但请注意,它仅适用于 iOS 8+,因为 NSLayoutConstraint
上的 active
属性 仅在 iOS 8 中添加.
首先确保您有一个 IBOutletCollection
数组,其中包含您需要的所有约束:
@IBOutlet var allConstraints : [NSLayoutConstraint]!
如果你有的话,你可以把它们连接到你的 xib/storyboard 中。否则只需在代码中设置它们。
然后,如果要隐藏单元格,请执行:
cell.allConstraints.forEach { [=11=].active = false }
在table查看数据源方法中-cellForRowAtIndexPath:
。
此外,重写单元格子类上的 -prepareForReuse:
方法,如下所示:
override func prepareForReuse() {
super.prepareForReuse()
allConstraints.forEach { [=12=].active = true }
}
这会重新启用约束,因此如果隐藏单元格被重新用于创建非隐藏单元格,事情就会正常运行。
您可能还需要添加对 setNeedsLayout
和 layoutIfNeeded
的调用,但我会尝试在没有它们的情况下开始 - 希望在 table 视图的 reloadData
应该为您处理事情 - 如果您不需要,最好不要调用这些函数。
尝试改变约束关系。而不是相等使用 less or equal
我在包含 UITextView 的表格视图中有一个单元格。 UITextview 对 uitableviewcell 内容视图有顶部、前导、尾部和底部的约束。如果文本视图包含空文本,我想隐藏 uitableviewcell。为此,我将单元格高度降低为 0。由于文本视图具有关于 UITableViewCell 的约束集。
UITableViewCell
---------------------------
| -T- |
| -L- UITextView -R- |
|_________-B-_____________|
L、T、B、R - 左、上、下、右约束
我遇到了约束问题。
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.
(
"<NSLayoutConstraint:0x7fe5d58753a0 UITableViewCellContentView:0x7fe5d5874fe0.bottomMargin == UITextView:0x7fe5d399c000.bottom>",
"<NSLayoutConstraint:0x7fe5d58753f0 UITableViewCellContentView:0x7fe5d5874fe0.topMargin == UITextView:0x7fe5d399c000.top>",
"<NSLayoutConstraint:0x7fe5d5888a20 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fe5d5874fe0(0.5)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fe5d58753a0 UITableViewCellContentView:0x7fe5d5874fe0.bottomMargin == UITextView:0x7fe5d399c000.bottom>
如何隐藏单元格而不影响自动布局。
您可以将字符串的值保存在全局变量中,然后在 cellForRowAtIndexPath 中检查 var 是否为空,如果不是,您可以 return 单元格。
像这样:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
if (your global var != nil) {
let cell: MyCell? = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as? MyCell
cell?.label.text = your value
return cell!
}
}
我会使用 active
属性 关闭和打开约束。这是最简单的方法,但请注意,它仅适用于 iOS 8+,因为 NSLayoutConstraint
上的 active
属性 仅在 iOS 8 中添加.
首先确保您有一个 IBOutletCollection
数组,其中包含您需要的所有约束:
@IBOutlet var allConstraints : [NSLayoutConstraint]!
如果你有的话,你可以把它们连接到你的 xib/storyboard 中。否则只需在代码中设置它们。
然后,如果要隐藏单元格,请执行:
cell.allConstraints.forEach { [=11=].active = false }
在table查看数据源方法中-cellForRowAtIndexPath:
。
此外,重写单元格子类上的 -prepareForReuse:
方法,如下所示:
override func prepareForReuse() {
super.prepareForReuse()
allConstraints.forEach { [=12=].active = true }
}
这会重新启用约束,因此如果隐藏单元格被重新用于创建非隐藏单元格,事情就会正常运行。
您可能还需要添加对 setNeedsLayout
和 layoutIfNeeded
的调用,但我会尝试在没有它们的情况下开始 - 希望在 table 视图的 reloadData
应该为您处理事情 - 如果您不需要,最好不要调用这些函数。
尝试改变约束关系。而不是相等使用 less or equal