1

I want to change some constraints aromatically in different cases.

Declaration of my Constraints coming from the storyboard:

    @IBOutlet weak var topConstraintPostImageView: NSLayoutConstraint!
    @IBOutlet weak var heightConstraint: NSLayoutConstraint!

This is my code, where I want to change the constraints:

func updateCellView(post: PostModel) {

    // Wenn Bild mit Text gepostet wird
    if post.imageURL != nil && post.postText != nil {

        topConstraintPostImageView.constant = 10

        // Wenn Text ohne bild gepostet wird
    } else if post.imageURL == nil && post.postText != nil  {

        heightConstraint.constant = 1

        // Wenn Bild ohne Text gepostet wird
    } else if post.imageURL != nil && post.postText == nil {

        topConstraintPostImageView.constant = 0

    }
}

But the constraints still doesn't change.

Here my cellForRowAt func:

    extension DiscoveryViewController: UITableViewDataSource {
    // wie viele Zellen
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return postArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DiscoveryCollectionViewCell", for: indexPath) as! DiscoveryCollectionViewCell

        cell.updateCellView(post: postArray[indexPath.row].post!)
        cell.layoutIfNeeded()
        cell.user = postArray[indexPath.row]
        cell.post = postArray[indexPath.row]
        cell.delegate = self

        return cell
    }
}

enter image description here

Thanks in advance for your help!

jo1995
  • 414
  • 1
  • 5
  • 14
  • Are you sure that at least one condition is `true`? – Robert Dresler Dec 18 '18 at 19:30
  • Yes the code is more complex, but I simplified for this question. All datas will be loaded but not the constraints. Do I have to make special settings for the constraints in storyboard? – jo1995 Dec 18 '18 at 19:32

2 Answers2

1

Insert in cellForRowAt in the vc

 cell.updateCellView(arr[indexPath.row])////  change arr to your datasource arr inside here set constants
 cell.layoutIfNeeded()
 return cell

at the end of the if statements

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Thanks for your answer. Im inside a tableViewCell, Value of type 'DiscoveryTableViewCell' has no member 'view'´´ – jo1995 Dec 18 '18 at 19:42
  • still the same problem but in the `VC``cellRowAt` `Value of type 'DiscoveryTableViewCell' has no member 'configure'` – jo1995 Dec 18 '18 at 19:57
  • see https://stackoverflow.com/questions/36574052/swift-dynamic-table-cell-height , – Shehata Gamal Dec 18 '18 at 20:00
  • screenshot the layout of the cell with constraints shown – Shehata Gamal Dec 18 '18 at 20:00
  • every if need a separate else to give the reverse value of constant when the condition doesn't fit , ex if image url exists then constant height say = 20 and if no then zero , do this for all constraints , as cells are dequeued – Shehata Gamal Dec 18 '18 at 20:11
0

Consider calling setNeedsUpdateConstraints() on your view from updateCellView() and then implement:

class MyView: UIView {
  // ...
  override func updateConstraints() {
    super.updateConstraints()
    // This is where you update your constraints including the logic above.
  }
  // ...
}

✳️ Better still, consider using UIStackViews to arrange and individually hide or show views you want to show conditionally.

Wolf McNally
  • 3,647
  • 1
  • 16
  • 13