这是我的例子,我无法判断这是否是一个错误。我的所有单元格都正确加载,但是当我尝试将其作为工作表打开时DetailView(),传入的项目始终是网格中首先显示的项目(在我的情况下位于左上角),而不是原来的“单元格”被点击。那么,为什么 ForEach 循环中的项目正确填充单元格,但没有通过按钮传递到 .sheet ?
import SwiftUI
let columnCount: Int = 11
let gridSpacing: CGFloat = 1
struct GridView: View {
@State var showingDetail = false
let data = (1...755).map { "\($0)" }
let columns: [GridItem] = Array(repeating: .init(.flexible(), spacing: gridSpacing), count: columnCount)
let colCount: CGFloat = CGFloat(columnCount)
var body: some View {
GeometryReader { geo in
ScrollView (showsIndicators: false) {
LazyVGrid(columns: columns, spacing: gridSpacing) {
ForEach(data, id: \.self) { item in
Button(action: {
self.showingDetail.toggle()
}) {
GridCell(item: …Run Code Online (Sandbox Code Playgroud) 我在尝试如何使用时惨遭失败.sheet(item:content:). 我知道这里和其他平台上有很多信息,但我就是无法让它发挥作用......
这是我想要 \xe2\x80\x93 的视图的抽象,我不知道我在这里做错了什么:
\nimport SwiftUI\n\nenum SheetView: String, Identifiable {\n var id: Self { self }\n case sheetA = "Sheet A"\n case sheetB = "Sheet B"\n}\n\nstruct SheetViewTest: View {\n @State private var showSheet: SheetView? = nil\n \n var body: some View {\n Form {\n Button(action: {\n showSheet = .sheetA\n print("Button \\(showSheet?.rawValue) tapped\xe2\x80\xa6")\n }, label: {\n Text(SheetView.sheetA.rawValue)\n })\n Button(action: {\n showSheet = .sheetB\n print("Button \\(showSheet?.rawValue) tapped\xe2\x80\xa6")\n }, label: {\n Text(SheetView.sheetB.rawValue)\n })\n }\n .sheet(item: $showSheet) { sheet …Run Code Online (Sandbox Code Playgroud)