一个有效的 ScrollView 示例和一个拒绝示例
One working ScrollView example and one refusing example
这是 ScrollView 的一个工作示例:
var body: some View {
ScrollViewReader { proxy in
VStack {
Button("Warp to line 10 on top") {
proxy.scrollTo(10, anchor: .top)
}
List(0..<100, id: \.self) { i in
HStack {
Text("Example \(i)")
.id(i)
Spacer()
Text("Example \(i)")
.id(i)
}
}
我尝试了另一个我想合并到我的应用程序中的示例,但该示例不起作用:
var body: some View {
ScrollViewReader { proxy in
VStack {
Button("Warp to line 10 on top") {
proxy.scrollTo(10, anchor: .top)
}
List {
ForEach(totalWeights, id: \.self) { totalWeight in
NavigationLink(destination: WeightDetailView()) {
HStack {
Text("Day: \(totalWeight.day)")
.foregroundColor(totalWeight.measured ? .green : .red)
Spacer()
Text("17 Apr")
Spacer()
Text("\(formatVar2(getal: totalWeight.weight)) gr")
Spacer()
Text("\(formatVar1(getal: totalWeight.prediction)) %")
}
}
}
}
我没看到什么?
根据xTwisteDx的评论,我做了另一个版本的代码。但是还是一样的问题...
ScrollViewReader { proxy in
VStack {
Button("Jump to #10") {
proxy.scrollTo(10, anchor: .top)
}
List {
ForEach(totalWeights, id: \.self) { totalWeight in
NavigationLink(destination: WeightDetailView()) {
HStack {
Text("Day: \(totalWeight.day)")
.foregroundColor(totalWeight.measured ? .green : .red)
.id(totalWeight.day)
Spacer()
Text("17 Apr")
.id(totalWeight.day)
Spacer()
Text("\(formatVar2(getal: totalWeight.weight)) gr")
.id(totalWeight.day)
Spacer()
Text("\(formatVar1(getal: totalWeight.prediction)) %")
.id(totalWeight.day)
.background(.thinMaterial, in: RoundedRectangle(cornerRadius: 10))
.padding(EdgeInsets(top: -20, leading: 0, bottom: 0, trailing: 0))
}
}
}
}
}
}
totalWeight
是 ActualWeight
类型,我让它可散列。同样是day: Int
,所以应该匹配代码中的10。如果单击按钮,我想将列表滚动到第 10 行。
struct ActualWeight: Hashable, Codable {
var day: Int
var weight: Double
var measured: Bool
var dailyLoss: Double
var weightLoss: Double
var prediction: Double
}
尝试关注
var body: some View {
ScrollViewReader { proxy in
VStack {
Button("Warp to line 10 on top") {
proxy.scrollTo(10, anchor: .top)
}
List {
ForEach(0..<totalWeights.count, id: \.self) { i in
NavigationLink(destination: WeightDetailView()) {
HStack {
Text("Day: \(totalWeights[i].day)")
.foregroundColor(totalWeights[i].measured ? .green : .red)
Spacer()
Text("17 Apr")
Spacer()
Text("\(formatVar2(getal: totalWeights[i].weight)) gr")
Spacer()
Text("\(formatVar1(getal: totalWeights[i].prediction)) %")
}
}
}
}
}
}
}
这是 ScrollView 的一个工作示例:
var body: some View {
ScrollViewReader { proxy in
VStack {
Button("Warp to line 10 on top") {
proxy.scrollTo(10, anchor: .top)
}
List(0..<100, id: \.self) { i in
HStack {
Text("Example \(i)")
.id(i)
Spacer()
Text("Example \(i)")
.id(i)
}
}
我尝试了另一个我想合并到我的应用程序中的示例,但该示例不起作用:
var body: some View {
ScrollViewReader { proxy in
VStack {
Button("Warp to line 10 on top") {
proxy.scrollTo(10, anchor: .top)
}
List {
ForEach(totalWeights, id: \.self) { totalWeight in
NavigationLink(destination: WeightDetailView()) {
HStack {
Text("Day: \(totalWeight.day)")
.foregroundColor(totalWeight.measured ? .green : .red)
Spacer()
Text("17 Apr")
Spacer()
Text("\(formatVar2(getal: totalWeight.weight)) gr")
Spacer()
Text("\(formatVar1(getal: totalWeight.prediction)) %")
}
}
}
}
我没看到什么?
根据xTwisteDx的评论,我做了另一个版本的代码。但是还是一样的问题...
ScrollViewReader { proxy in
VStack {
Button("Jump to #10") {
proxy.scrollTo(10, anchor: .top)
}
List {
ForEach(totalWeights, id: \.self) { totalWeight in
NavigationLink(destination: WeightDetailView()) {
HStack {
Text("Day: \(totalWeight.day)")
.foregroundColor(totalWeight.measured ? .green : .red)
.id(totalWeight.day)
Spacer()
Text("17 Apr")
.id(totalWeight.day)
Spacer()
Text("\(formatVar2(getal: totalWeight.weight)) gr")
.id(totalWeight.day)
Spacer()
Text("\(formatVar1(getal: totalWeight.prediction)) %")
.id(totalWeight.day)
.background(.thinMaterial, in: RoundedRectangle(cornerRadius: 10))
.padding(EdgeInsets(top: -20, leading: 0, bottom: 0, trailing: 0))
}
}
}
}
}
}
totalWeight
是 ActualWeight
类型,我让它可散列。同样是day: Int
,所以应该匹配代码中的10。如果单击按钮,我想将列表滚动到第 10 行。
struct ActualWeight: Hashable, Codable {
var day: Int
var weight: Double
var measured: Bool
var dailyLoss: Double
var weightLoss: Double
var prediction: Double
}
尝试关注
var body: some View {
ScrollViewReader { proxy in
VStack {
Button("Warp to line 10 on top") {
proxy.scrollTo(10, anchor: .top)
}
List {
ForEach(0..<totalWeights.count, id: \.self) { i in
NavigationLink(destination: WeightDetailView()) {
HStack {
Text("Day: \(totalWeights[i].day)")
.foregroundColor(totalWeights[i].measured ? .green : .red)
Spacer()
Text("17 Apr")
Spacer()
Text("\(formatVar2(getal: totalWeights[i].weight)) gr")
Spacer()
Text("\(formatVar1(getal: totalWeights[i].prediction)) %")
}
}
}
}
}
}
}