Reputation: 1305
I have a cell in a tableview which contains UITextView. UITextview has a constraints of top, leading, trailing and bottom to the uitableviewcell content view. I want to hide the uitableviewcell if a textview contains empty text. For that, i reduce cell height to 0. Since the textview has constraint set with respect to UITableViewCell.
UITableViewCell
---------------------------
| -T- |
| -L- UITextView -R- |
|_________-B-_____________|
L,T,B,R - Left, Top, Bottom, Right Constraints
i am getting the constraints issue.
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>
How can i hide the cell without having any issue with autolayout.
Upvotes: 2
Views: 317
Reputation: 213
Try to change the relation of the constraints. Instead of equal use less or equal
Upvotes: 0
Reputation: 3842
I would turn the constraints off and on using the active
property. This is the simplest approach, but note that it will only work on iOS 8+, because the active
property on NSLayoutConstraint
was only added in iOS 8.
First ensure that you have an IBOutletCollection
array containing all the constraints you need:
@IBOutlet var allConstraints : [NSLayoutConstraint]!
You can hook them up in your xib/storyboard, if you have one. Otherwise just set them up in code.
Then, in the case where you want to hide the cell, do:
cell.allConstraints.forEach { $0.active = false }
in the tableview datasource method -cellForRowAtIndexPath:
.
Also, override the -prepareForReuse:
method on the cell subclass as follows:
override func prepareForReuse() {
super.prepareForReuse()
allConstraints.forEach { $0.active = true }
}
This turns the constraints back on, so things behave correctly if a hidden cell is reused to create a non-hidden cell.
You may also need to add calls to setNeedsLayout
and layoutIfNeeded
, but I'd try without them to start with - hopefully the layout pass triggered in the table view's reloadData
should take care of things for you - it's always best not to call those functions if you don't need to.
Upvotes: 1
Reputation: 465
You can save value of the string in a global variable, then in the cellForRowAtIndexPath you can check if var is null, if not you can return cell.
Like this:
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!
}
}
Upvotes: 0