首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SwiftUI :我如何在VStack中删除行ForEach?

SwiftUI :我如何在VStack中删除行ForEach?
EN

Stack Overflow用户
提问于 2022-04-07 16:05:15
回答 1查看 267关注 0票数 0

我是TaskModel

代码语言:javascript
运行
复制
import Foundation
import SwiftUI


struct TaskModel: Identifiable, Codable {
    var id = UUID().uuidString
    var title : String
    var selectedColor : Color
    var remindedTime : Int
    var taskDate : Date
}

这是我试过的ViewModel

代码语言:javascript
运行
复制
    import Foundation
    import SwiftUI
    
    class CalendarViewModel : ObservableObject {
        
        @Published var tasks : [TaskModel] = []
        @Published var currentMonth : Int = 0
        
    
        func addTask(title : String, selectedColor : Color, reminderTime : Int, taskDate : Date) {
            let newTask = TaskModel(title: title, selectedColor: selectedColor, remindedTime: reminderTime * 60, taskDate: taskDate)
            tasks.append(newTask)
        }
        
        func deleteTask(task : TaskModel) {
            if let index = tasks.firstIndex(where: {$0.id == task.id }) {
                tasks.remove(at: index)
            }
        }

这是我试图删除的行的ForEach。

代码语言:javascript
运行
复制
VStack {
                    ForEach(vm.tasks.filter({vm.isSameDay(date1: $0.taskDate, date2: currentDate)})) { task in
                        TaskRowView(task: task)
                            .actionSheet(isPresented: $isShowActionSheet) {
                                ActionSheet(title: Text("Settings"), message: Text("Press the button that what you want to do "), buttons: [
                                    .cancel(), .destructive(Text("Delete"), action: {
                                        vm.deleteTask(task: task)
                                    }), .default(Text("Edit"), action: {
                                        
                                    })
                                ])
                            }
                    }
                    
                    .onLongPressGesture {
                        isShowActionSheet.toggle()
                    }
                 }

这是我的应用程序视图的照片。

奇怪的是,当我尝试删除列表的第二行时,第一行被删除,第二行被留下。

但是,我找不到我的失意点。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-07 22:17:20

实现这一点的最简单方法是使用可选的选择变量和.actionSheet()的自定义绑定。请看下面的代码。我评论了重要的部分。为了演示目的,我还将您的代码更改为Minimal Reproducible Example (MRE)

代码语言:javascript
运行
复制
struct ContentView: View {
    
    @State var tasks = Array(1...20).map( { TaskModel(title: "Task \($0)") })
    // This is your optional selection variable
    @State var selectedRow: TaskModel?
    
    var body: some View {
        VStack {
            ForEach(tasks) { task in
                Text(task.title)
                    // put the .onLongPressGesture() on the row itself
                    .onLongPressGesture {
                        // set selectedRow to the task
                        selectedRow = task
                    }
                    // This creates a custom binding
                    .actionSheet(isPresented: Binding<Bool>(
                        // the get returns the Bool that is the comparison of selectedRow and task
                        // if they are equal, the .actionSheet() fires
                        get: { selectedRow == task },
                        // when done, .actionSheet() sets the Binding<Bool> to false, but we intercept that
                        // and use it to set the selectedRow back to nil
                        set: { if !$0 {
                            selectedRow = nil
                        }})) {
                        ActionSheet(
                            title: Text("Settings"),
                            // added task.title to prove the correct row is in the ActionSheet
                            message: Text("Press the button for \(task.title)"),
                            buttons: [
                                .cancel(),
                                .destructive(Text("Delete"), action: {}),
                                .default(Text("Edit"), action: {})
                            ]
                        )
                    }
            }
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71785474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档