0

I'm creating an expandable cell with labels and picker view

xib

enter image description here

Cell Implementation

class PickerViewCell: UITableViewCell {

@IBOutlet weak var detailLabel: UILabel!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var height: NSLayoutConstraint!
@IBOutlet weak var picker: UIDatePicker!
var isExpanded:Bool = false
    {
        didSet
        {
            if isExpanded == false{
                self.height.constant = 0.0
                self.picker.hidden = true

            }
            else{
                self.height.constant = 200.0
                self.picker.hidden = false
            }
        }
}
override func awakeFromNib() {
    self.height.constant = 0.0
    super.awakeFromNib()
}

Delegate method

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    guard let cell = tableView.cellForRowAtIndexPath(indexPath) as? PickerViewCell else { return }

    cell.isExpanded = !cell.isExpanded
    print(cell.isExpanded)
    UIView.animateWithDuration(0.3) {
        cell.contentView.layoutIfNeeded()
    }

    tableView.beginUpdates()
    tableView.endUpdates()

    tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: true)
}

I keep getting runtime warining about breaking constraints.

enter image description here

I set up a symbolic breakpoint and what I found out is that this line

self.height.constant = 200.0

inside cell implementation is the reason of the warning.

However, I found out that it happens only during the first tap on a given cell, after the initial tap I can tap multiple times, expanding and closing it each time, and the warning does not occur. Should I even care about this runtime warning?

EDIT:

I forgot to show datePicker constraints including those on height and aspect ratio:

enter image description here

Also it's worth to mention that I utilize

self.tableView.estimatedRowHeight = 100.0
self.tableView.rowHeight = UITableViewAutomaticDimension

in my viewController

theDC
  • 6,364
  • 10
  • 56
  • 98
  • i'm guessing the problem is with the uipickerview constraints, currently it's configured to be 10 point == to bottom layout guide and it's top == to the label.bottom, i think it's Ambiguous because of the equality constraint,try to configure priority in one of them – Dasem Aug 22 '16 at 15:10
  • Issue is about height constraint – theDC Aug 22 '16 at 15:15

2 Answers2

0

You set datePicker height to 200, but then set cell height to 200 too. But cell height is datePicker's height + 10 + label's height. So those constraints conflict to each other.

Sunny
  • 137
  • 7
  • I named picker's constraint 'height – theDC Aug 22 '16 at 15:40
  • Sorry, I didn't catch this. But according to your screenshot problem is that datePicker's height doesn't feet into cell (cell height is 66.5). My advice - to not use UITableViewAutomaticDimension for row height, and use condition like cell.isExpanded ? closedCellHeight : openedCellHeight in heightForRowAtIndexPath method. – Sunny Aug 22 '16 at 15:51
  • well this does not work I guess, however you may try to post the code in the answer, maybe I wrongly applied it – theDC Aug 22 '16 at 20:57
0

I tried before to lower the priority of height constraint to 750 and lower, that did not work.

However, thanks to this answer https://stackoverflow.com/a/30333810/4286947 I set it equal to 999 and this turns out to be the solution here

Community
  • 1
  • 1
theDC
  • 6,364
  • 10
  • 56
  • 98