滑动以仅删除 SwiftUI 列表中的选定行?

nic*_*rno 6 xcode swift swiftui swiftui-list

下面是 SwiftUI 列表的基本代码,它允许在所有行上使用滑动删除功能。是否可以将其限制为特定的行/索引?(即只允许第三行上的滑动删除行为,但在所有其他行上禁用它?)

在我的应用程序中,我有一个评论列表,我希望用户能够滑动删除自己的评论,但应该在所有其他评论上禁用滑动删除行为。我知道在 UIKit 中使用 UITableView 委托之一是可能的,但我不确定如何在 SwiftUI 中实现它。

struct ListTest: View {
    var body: some View {
        List {
            ForEach(0..<50) { index in
                Text("index is \(index)")
            }
            .onDelete(perform: { indexSet in
                print(indexSet)
            })
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Asp*_*eri 8

这是一个解决方案。使用 Xcode 12.1 / iOS 14.1 进行测试

    List {
        ForEach(0..<50) { index in
             Text("index is \(index)")
                .deleteDisabled(index == 3)       // << ex. !!
        }
        .onDelete(perform: { indexSet in
            print(indexSet)
        })
    }
Run Code Online (Sandbox Code Playgroud)