【问题标题】:SwiftUI: Edit list row relement with swipe action and present modal sheetSwiftUI:使用滑动操作编辑列表行元素并显示模式表
【发布时间】:2022-06-17 16:35:01
【问题描述】:

嗨。我有一个带有列表的 swiftui 项目。我现在想在此列表中添加 2 个尾随滑动操作,一旦 .onDelete 和一个编辑滑动操作到它的左侧。 像这样: Look:

为了在 swiftui 中实现这一点,我在列表中添加了以下代码:

            List {
                
                ForEach(timers, id: \.id) { timer in
                                        
                    TimerRow(timer: timer)
                }
                .onDelete(perform: { IndexSet in deleteTimer(IndexSet) })
                .swipeActions(edge: .trailing, allowsFullSwipe: false) {
                    
                    Button {
                        // Open edit sheet
                        isShowEditTimer.toggle()
                        
                    } label: {
                        
                        Image(systemName: "pencil.circle")
                    }
                }
            }

可惜现在只显示编辑功能:

Look ????

你知道我该如何解决我的问题吗?

但现在我真正的问题是: 我现在想在按下一行的编辑滑动操作时打开一个模式表。但是我怎么知道哪条线被刷了呢?使用 .onDelete 函数,我们得到一个 IndexSet,但这里什么都没有。我还想给在我的工作表中调用的结构这个特定的滑动元素(CoreData 对象):

        .sheet(isPresented: $isShowEditTimer) {
            EditTimerView(timerObject: ???)
        }

顺便说一下,这张表被应用到我的导航视图中。

如果有人可以帮助我并且你没有举报我的帖子,我会非常高兴。也许这个问题已经在 StackOverflow 的某个深处被问过,但我对 swiftui 也比较陌生(以前总是 UIKit),还不了解每个 stackoverflow 帖子。

谢谢!!! ????

【问题讨论】:

  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: swift xcode list swiftui


【解决方案1】:

您好 Eric,欢迎来到 SO!如果您想获得问题的答案,请阅读这篇文章https://stackoverflow.com/help/minimal-reproducible-example

无论如何,我遇到了与您描述的相同的问题,如果您不介意,我将在我的代码库中展示如何解决它

import SwiftUI

struct Car: Identifiable {
    let id = UUID().uuidString
    var model: String
}

class CarViewModel: ObservableObject {
    @Published var cars: [Car] = [
        .init(model: "Audi"),
        .init(model: "BMW"),
        .init(model: "Chevrolet"),
        .init(model: "Honda"),
        .init(model: "Toyota")
    ]
    
    func del(_ car: Car) {
        if let deletingCarIndex = self.cars.firstIndex(where: { $0.id == car.id}) {
            self.cars.remove(at: deletingCarIndex)
        }
    }
}

struct ContentView: View {
    
    @ObservedObject var vm = CarViewModel()
    @State var editingCar: Car?
    
    var body: some View {
        List {
            ForEach(vm.cars) { car in
                Text(car.model)
                    .swipeActions(edge: .trailing) {
                        Button(role: .destructive) { // if you implement custom swipeAction you have no more access to .onDelete
                            vm.del(car) // if you stay in ForEach loop you have access to car instance and can find index of this instance in array
                        } label: {
                            Label("Delete", systemImage: "trash")
                        }
                        Button {
                            edit(car)
                        } label: {
                            Label("Edit", systemImage: "pencil.circle")
                        }
                    }
            }
            .sheet(item: $editingCar) {
                reset()
            } content: { car in
                VStack(spacing: 20) {
                    Text(car.model)
                    Text(car.id)
                }
            }
            
        }
    }
    
    func reset() {
        editingCar = nil
    }
    
    func edit(_ car: Car) {
        editingCar = car
    }
}

希望现在你明白了?

【讨论】:

    猜你喜欢
    • 2019-11-28
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2020-05-08
    • 2014-08-07
    • 1970-01-01
    相关资源
    最近更新 更多