这是我的示例,我无法判断这是否是错误。我的所有单元格都正确加载,但是当我尝试将其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: item, size: (geo.size.width - (colCount * gridSpacing)) / colCount)
}.sheet(isPresented: $showingDetail) {
DetailView(item: item)
}
}
}
}
.padding(.horizontal, gridSpacing)
}
}
}
struct GridCell: View {
let isVault: Bool = false
let item: String
let size: CGFloat
var body: some View {
ZStack {
Rectangle()
.fill(Color.orange)
.cornerRadius(4)
Text(item)
.foregroundColor(.white)
.font(.system(size: size * 0.55))
}
.frame(height: size)
}
}
struct DetailView: View {
let item: String
var body: some View {
Text(item)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
GridView()
.preferredColorScheme(.dark)
}
}
我错过了什么?另外,在我的 iPad pro 11 上滚动有点跳动,有没有人看到同样的行为?