Disable "swipeActions" on only a specific row in a List

Hi, I want to enable swipeActions for some rows of a List while other rows do not have this action.

Here is an example:

List {
    ForEach(1..<100) { i in
        Text("\(i)")
            .swipeActions(edge: .leading) {
                Button {
                    total += i
                } label: {
                    Label("Add \(i)", systemImage: "plus.circle")
                }
                .tint(.indigo)
            }
            .swipeActions(edge: .trailing) {
                Button {
                    total -= i
                } label: {
                    Label("Subtract \(i)", systemImage: "minus.circle")
                }
            }
    }
}

What would be the best approach to disable the swipeActions for let say the 50 row?

Answered by Claude31 in 771248022

You can use disabled modifier:

struct ContentView: View {
    @State var total = 0
    
    var body: some View {
        Text("Total \(total)")
        List {
            ForEach(1..<100) { i in
                Text("\(i)")
                    .listRowBackground((i > 3 && i < 8) ? Color.gray.opacity(0.2) : .white). // If you want to gray out disabled rows
                    .swipeActions(edge: .leading) {
                        Button {
                            total += i
                        } label: {
                            Label("Add \(i)", systemImage: "plus.circle")
                        }
                        .disabled(i > 3 && i < 8)    // Whatever value you need here
                        .tint(.indigo)
                    }
                    .swipeActions(edge: .trailing) {
                        Button {
                            total -= i
                        } label: {
                            Label("Subtract \(i)", systemImage: "minus.circle")
                        }
                        .disabled(i > 3 && i < 8)    // Whatever value you need here
                    }
            }
        }
    }
}

If that's OK, don't forget to close the thread by marking the correct answer. Otherwise, please explain what you need.

Accepted Answer

You can use disabled modifier:

struct ContentView: View {
    @State var total = 0
    
    var body: some View {
        Text("Total \(total)")
        List {
            ForEach(1..<100) { i in
                Text("\(i)")
                    .listRowBackground((i > 3 && i < 8) ? Color.gray.opacity(0.2) : .white). // If you want to gray out disabled rows
                    .swipeActions(edge: .leading) {
                        Button {
                            total += i
                        } label: {
                            Label("Add \(i)", systemImage: "plus.circle")
                        }
                        .disabled(i > 3 && i < 8)    // Whatever value you need here
                        .tint(.indigo)
                    }
                    .swipeActions(edge: .trailing) {
                        Button {
                            total -= i
                        } label: {
                            Label("Subtract \(i)", systemImage: "minus.circle")
                        }
                        .disabled(i > 3 && i < 8)    // Whatever value you need here
                    }
            }
        }
    }
}

If that's OK, don't forget to close the thread by marking the correct answer. Otherwise, please explain what you need.

Disable "swipeActions" on only a specific row in a List
 
 
Q