1
votes

I'm having trouble changing the relation on a constraint from storyboard. This is how the table view looks like at the moment. enter image description here

When the more button is click, I want the table view to look like this. enter image description here

Okay, so I drag the bottom constraint from the imageView into my tableViewCell.swift file and named it imageBottomConstraint. I tried imageBottomConstraint.relation = .greaterThan but gives me an error saying that it's only a get, so the relation is equalTo and I'm trying to change it to greaterThan.

Here is were I'm confused and stuck, so I'm trying to create a new constraint but don't really understand NSLayoutConstraint(item:, attribute:, relatedBy:, toItem:, attribute:, multiplier:, constant:). Don't understand item, attribute, relatedBy, toItem & attribute, can someone please help clarify.

My disaster code I thought would work but nope crashes instead.

@IBAction func showMoreBtn() {
    let item = imageBottomConstraint.firstItem
    let attribute1 = imageBottomConstraint.firstAttribute
    let relatedBy = NSLayoutRelation.greaterThanOrEqual
    let item2 = imageBottomConstraint.secondItem
    let attribute2 = imageBottomConstraint.secondAttribute
    let multiplier = imageBottomConstraint.multiplier
    let constant = 12 

    let newConstraint = NSLayoutConstraint(item: item, attribute: attribute1, relatedBy: relatedBy, toItem: toItem, attribute: attribute2, multiplier: multiplier, constant: 12)
}

So as you can see I don't know what I am doing, when creating constraint programmatic. Would appreciate the help.

2

2 Answers

1
votes

You can Manage the Expected Behaviour in heightForRowAt Cell Delegate of TableView

/// Use this function to Set Height of TableCells
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
        {
            /// Is index Path More Button is cliked ?
            if indexPath.row == selectedIndex
            {
                /// If yes
                return UITableViewAutomaticDimension
            }
            else
            {
                /// if No
                /// Provide a Static Height That will Set your requiremment
                return 160
            }
        }

selectedIndex its the index in which that more Button is Clicked , My requirement was to Expand one cell At a time, You can use a Array to manange the cells Need to be Expanded

0
votes

You might not use the right solution. I would use the fact that cells can expand and adapt to there content by using UITableViewAutomaticDimension. Here is a sample that could help you on your particular case:

https://www.appcoda.com/self-sizing-cells/

The idea would be:

  • fix with a constraint the height of your label
  • When you click the more button, you set that constraint to not active with yourConstraint.active = false and reload the cell by doing something like yourTableView.reloadRows