How to auto adjust the height of list row in edit mode?

I have a list. When I click the show button in any of rows, the long text appears and the height of row auto gets taller enough to show all of the long text. However, when the list is in "edit" mode, the height of row can't auto get taller enough to show all of the long text. The long text is truncated. I have to push the list to the top manually and release, then the height of row auto gets taller and the long text can be completely displayed again. Is there any way to achieve the "auto" adjustment effect just the same as the "non-edit" mode?

struct ListEditTest: View {
    @State private var selects: Set<Int> = []
    var body: some View {
        VStack{
            EditButton()
            List(selection: $selects){
                ForEach(1..<5){
                    ItemInList(num: $0)
                }
            }
            .listStyle(PlainListStyle())
        }
    }
}
struct ItemInList: View{
    let num: Int
    @State private var showDetail = false
    var body: some View{
        HStack{
            Text("NO.\(num)")
            Text("HelloWorld")
            if showDetail{
                Text("TooManyWordsTooManyWordsTooManyWordsTooManyWordsTooManyWordsTooManyWordsTooManyWordsTooManyWords")
                Button {
                    showDetail.toggle()
                } label: {
                    Text("Hide")
                }
            }else{
                Button {
                    showDetail.toggle()
                } label: {
                    Text("Show")
                }
            }
        }
        .buttonStyle(BorderlessButtonStyle())
    }
}
  • If you show detail then Edit, you get the multilines.

  • Could this help ? https://www.hackingwithswift.com/forums/swiftui/update-content-of-existing-row-of-list/3029

  • Yes, the issue only happens when I tap the button in edit mode. I will read the linked post, thank you.

Add a Comment

Replies

Replace your ListEditTest file with this code or just add the .frame() part. I've changed everything to work in your file just replace the comment with an INT.

import SwiftUI

struct ListEditTest: View {
    @State private var selects: Set<Int> = []
        var body: some View {
            VStack{
                EditButton()
                List(selection: $selects){
                    ForEach(1..<5){
                        ItemInList(num: $0)
                    }
                    .frame(height: /*Preferred height of each item as an INT*/)
                }
                .listStyle(PlainListStyle())
            }
        }
}
  • I'm afraid I can't indicate the exact height of the long text needed.

  • The long text will be loaded dynamically and each row may need different height. The height is undetermined.

  • I would make a Int var called textHeight and inside of the ForEach loop set textHeight to a value based on the length of the item

Add a Comment