'SwiftUI: Update height of an existing row in list

I'm new to programming in SwiftUI. I created a list of a custom view that expands on tap. However, whenever I tap the custom view, the height of the list row doesn't update. Here's what my code looks like:

       List(){
            
             ForEach(ingredients){ ingredient in
                IngredientList(ingredient: ingredient)
            }
            
        }

        struct IngredientList: View {
           let ingredient: Ingredient
          @State private var showBody = true
          
          var body: some View {      
             Hstack{
                VStack(alignment: .leading){
                   Text("Text 1")
                
                   if(showBody){
                       Divider()
                       Text("Text 2")
                           .font(.caption)
                    }
                 }
              }
               .contentShape(Rectangle())
               .onTapGesture {
                   showBody.toggle()
                }
             }
          }

I think it has something to do with state of the list but I can't seem to figure it out.



Solution 1:[1]

I found an ugly but working solution. On the expanded item change, check whether it is a selection mode, remember the current data set, clear it, and set it back with DispatchQueue.main.async. It works on iOS 15.4.1. I hope Apple will fix the issue in the subsequent releases.

Update: try introspect. You will be able to retrieve the underlying table view and call UITableView.reloadData() on the expanded row change

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1