How to disable some cells in a List in SwiftUI?

I have a List. I want to disable first cell. How can I do it?


I set

.disabled(true)
but it doesn't work.
struct ContentView: View {
var body: some View {
List {
ForEach(1..<5) {
Text("\($0)")
.disabled($0 == 1)
}
}
}
}

Replies

That's nearly OK, but you have no action to disable !


Consider and test the following:


struct ContentView: View {
    let l = ["a", "b", "c"]
  
    var body: some View {
        NavigationView {
            List {
                ForEach (l, id: \.self) {l in
                    NavigationLink(destination: sv()) {
                        Text(l)
                    }.navigationBarTitle(Text("List")).disabled(l == "b")
                }
            }
        }.navigationViewStyle(DoubleColumnNavigationViewStyle())
            .padding()
      
    }
}

struct sv: View {
    var body: some View {
        Text("Hello, World!")
    }
}

I don't need a NavigationLink. I use Button to trigger some REST API call.


I want a List' row to not shrink when touched. How can I do it?


List {
    ForEach(store.devices) { device in
        DeviceCell(device: device) {
            self.store.openDevice(id: device.id)
        }.disabled(!device.isActive)
    }
}

Why does a cell shrink when you touch ?


What happens for those not disabled ?


If you showed code of DeviceCell, may be there is a way to do it there with a @State var.

I simplifed my code.


I have a List with 6 buttons. I want disable first button. I set `disabled(false`. The first button is disabled — an action doesn't trigger but when I touch the button it shrinks. How to disable shrinking?


import SwiftUI

struct ContentView: View {
    var body: some View {
        List {
            ForEach(1..<7) { elem in
                Button(action: {
                    print(1)
                }, label: {
                    Text("\(elem)")
                })
                .disabled(elem == 1)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I do not understand what you mean by shrinking.


I tested.

When I tap on button 1, get nothing at all

On others, cell turn grey and I get a print on log.


Never any "shrinking".


Note: I slightly modified code to make it easier to understand what happens:


                Button(action: {
                    print("Hit", elem)
                }, label: {
                    Text("Button \(elem)")
                })


So please explain.

Sorry. I develop SwiftUI app for watchOS.


When a button in a List touched it scale down.

Abel9.png


I want to disable a button so it doesn't scale down on touch.

I cannot se the scale down on the image. Just see that text is dimmed and thus appears smaller. But there is no scale down.

Take a closer look. The first cell has a smaller width when pressed than the second.

https://drive.google.com/file/d/15d6e-YPGGS-AC26EVyBCVejdNiVYpWn1/view?usp=sharing