skip to Main Content

I have a tableView and cells. The Cells are loaded from a xib and they have a label with automatic height. I need to narrow one cell if the user taps on it.

I have tried hiding – doesn’t work
I have tried removeFromSuperView()– doesn’t work

Is there any alternative?

enter image description here

2

Answers


  1. Did you try to do something like this:

        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        var result: CGFloat
        if (indexPath.row==0) {
            result = 50 }
        else {result = 130}
        return result
    }
    

    This is just an example where height is changed for the first row. I tested on my application and it gave result like this.
    enter image description here

    Login or Signup to reply.
  2. When setting up your tableViewCell store the height anchor you want to update

    var yourLabelHeightAnchor: NSLayoutConstraint?
    private func setupLayout() {
         yourLabelHeightAnchor = yourLabel.heightAnchor.constraint(equalToConstant: 50)
         // Deactivate your height anchor as you want first the content to determine the height
         yourLabelHeightAnchor?.isActive = false
    }
    

    When the user clicks on a cell, notify the tableView that the cell is going to change, and activate the height anchor of your cell.

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         let cell = tableView.dequeueReusableCell(withIdentifier: "YourTableViewCellIdentifier") as? YourCell
         self.tableView.beginUpdates()
         cell?.yourLabelHeightAnchor?.isActive = true
         self.tableView.endUpdates()
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search