Dynamic Cell Height?

Hi,

I have tableview and in the tableview cell,i placed a plus button and minus button.

Tapping on the plus button should insert a view below the button/bottom of the cell.

if we tap the plus button second time,one more view has to be inserted below the first view.

Tapping on the minus should remove the view last inserted.

So, It has to increase and decrease the tableview cell height dynamically.

How to achieve this in swift?

Is this possible?

Thanks in advance.

Replies

You should implement the optional delegate func:


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat


Should look like

You define an expanded property for the cell, to keep track of the addition or deletion of the additional view


    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        var rowHeight: CGFloat = 30.0

        if expanded {
            rowHeight = 50.0
        } 

        return rowHeight
    }

Hi Claude31,

I thought that i have to add a tableview inside a cell?

can you able to clarify me little bit more?

Thank u very much for your support.

Why do you want to add a tableView inside a cell.


It is the cell that is inside a tableView.


What is it you don't understand ?


Would be better to post the complete class code and mark where you have problems (don't forget to edit the code with <> tool, to be able to reference statements by line number.