Best approach for adding subviews in tableview cell at runtime and expand tableview cell

Viewed 159

enter image description here enter image description here

I have a table view (picture attached) where I have a cell and its status(multiple).

When a user clicks the cell it expands show the status.

My question is what is the best approach to achieve this functionality, I don't feel like the one I have right is a good approach.

Right now when a user click I set a variable in didSelectRowAt and reload the cell. When I reload the cell I check the variable and add/remove subview.

I'm adding subview in a stackview like this:-

    for _ in 1...4 {
        let view = ActivityFeedStatusView()
        let constraint = NSLayoutConstraint(item: view, attribute: .height, relatedBy: .equal,
                                            toItem: nil, attribute: .height, multiplier: 1.0, constant: 70.0)
        NSLayoutConstraint.activate([constraint])
        stackview.addArrangedSubview(view)
    }

As you can see it'll get added all the time cellForRowAt will be called plus It is also giving me constraint warning.

I'm open to any kind of feedback and suggestions, thank you for reading.

Note: This code is just a test code to generate and show view

1 Answers

Your solution seems fine to me: My complete solution is: 1.keep track of previous selected cell index and current selected cell index by delegate method didSelectCellAtIndex. 2.reload cells at previous cell index and current cell index. 3.in cell init codes.if it is at previous then remove all status subviews.Otherwise,add status subviews on this cell. 4.manually calculate the height and reload or use auto layout for self height constraint.

Related