在 SwiftUI 中为 TotalValue 添加列表值
Adding List Values in SwiftUI for TotalValue
在这里提问
在学习SwiftUI的过程中,这个app是专门针对WatchOS的。所以我正在创建一个行视图,然后使用轮播视图。我有它,所以如果你点击列表项,它会要求你输入你的分数。那,当然,进入一个字符串(我希望我能把它变成一个整数,但对我来说行不通。)帧得分显示很好。但是,我正在尝试找出一种方法来正确添加总分。
例如,
第1局得分5总分5
第2局得分2总分7
第3局得分10总分17
...
任何帮助将不胜感激谢谢
struct StartBowlingView: View {
var body: some View {
List{
RowView(title: "1", framescore: "0", totalscore: "0")
RowView(title: "2", framescore: "0", totalscore: "0")
RowView(title: "3", framescore: "0", totalscore: "0")
RowView(title: "4", framescore: "0", totalscore: "0")
RowView(title: "5", framescore: "0", totalscore: "0")
RowView(title: "6", framescore: "0", totalscore: "0")
RowView(title: "7", framescore: "0", totalscore: "0")
RowView(title: "8", framescore: "0", totalscore: "0")
RowView(title: "9", framescore: "0", totalscore: "0")
RowView(title: "10", framescore: "0", totalscore: "0")
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
.navigationBarTitle("Frame")
.listStyle(CarouselListStyle())
}
}
struct RowView: View {
@State var title: String
@State var framescore: String
@State var totalscore: String
var TotalScore: Int {
let Totalframescore = Int(framescore) ?? 0
return Totalframescore
}
var body: some View {
NavigationLink(destination: TextField("Enter your Frame Score", text: $framescore) .border(Color.black))
{ //Start Frame List Design View
VStack(alignment: .leading) {
HStack(alignment: .center) {
Text(title)
.font(.system(.headline, design: .rounded))
.foregroundColor(Color.blue)
.multilineTextAlignment(.leading)
Divider()
Spacer()
Text("\(framescore)")
.font(.system(.body, design: .rounded))
.multilineTextAlignment(.leading)
Divider()
Spacer()
Text("\(TotalScore)")
.font(.system(.headline, design: .rounded))
.foregroundColor(Color.green)
.multilineTextAlignment(.trailing)
}
}
.listRowBackground(Color.blue)
.frame(height: 60, alignment: .topTrailing)
}
}
}
一种方法可以在每一行上减少到该点为止的分数集。通过遍历分数集,它可以计算帧数、帧分数和先前分数的总和。例如,您的主视图可能如下所示:
struct StartBowlingView: View {
@State var frameScores = [5, 2, 10]
var body: some View {
List{
ForEach(0..<self.frameScores.endIndex) { index in
RowView(title: "\(index + 1)",
framescore: "\(self.frameScores[index])",
totalscore: "\(self.frameScores[0...index].reduce(0, { [=10=] + }))")
}
}
}
}
我意识到保龄球得分可能比简单地对帧求和更复杂(虽然我不知道确切的逻辑),但另一种方法可能是创建一个帧模型来跟踪多个 Int:
struct FrameScore {
let frameNumber: Int
let firstScore: Int
let secondScore: Int
let previousScore: Int
var frameScore: Int { return self.firstScore + self.secondScore }
var totalScore: Int { return self.frameScore + self.previousScore }
var isStrike: Bool { return self.firstScore == 10 }
var isSpare: Bool { return !self.isStrike && self.frameScore == 10 }
}
然后主视图可以更新以保留帧列表:
struct StartBowlingView: View {
@State var frames = [FrameScore]()
var body: some View {
List{
ForEach(self.frames, id: \.frameNumber) { frame in
RowView(title: "\(frame.frameNumber)",
framescore: "\(frame.frameScore)",
totalscore: "\(frame.totalScore)")
}
Button("Add Score") {
let first = Int.random(in: 0...10)
let second = Int.random(in: 0...(10 - first))
// Here's where to add some logic about the prevous frames' strikes and spares affecting the new frame/total score
self.frames.append(
FrameScore(frameNumber: self.frames.count + 1,
firstScore: first,
secondScore: second,
previousScore: self.frames.last?.totalScore ?? 0))
}
}
}
}
在这里提问 在学习SwiftUI的过程中,这个app是专门针对WatchOS的。所以我正在创建一个行视图,然后使用轮播视图。我有它,所以如果你点击列表项,它会要求你输入你的分数。那,当然,进入一个字符串(我希望我能把它变成一个整数,但对我来说行不通。)帧得分显示很好。但是,我正在尝试找出一种方法来正确添加总分。
例如,
第1局得分5总分5
第2局得分2总分7
第3局得分10总分17
...
任何帮助将不胜感激谢谢
struct StartBowlingView: View {
var body: some View {
List{
RowView(title: "1", framescore: "0", totalscore: "0")
RowView(title: "2", framescore: "0", totalscore: "0")
RowView(title: "3", framescore: "0", totalscore: "0")
RowView(title: "4", framescore: "0", totalscore: "0")
RowView(title: "5", framescore: "0", totalscore: "0")
RowView(title: "6", framescore: "0", totalscore: "0")
RowView(title: "7", framescore: "0", totalscore: "0")
RowView(title: "8", framescore: "0", totalscore: "0")
RowView(title: "9", framescore: "0", totalscore: "0")
RowView(title: "10", framescore: "0", totalscore: "0")
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
.navigationBarTitle("Frame")
.listStyle(CarouselListStyle())
}
}
struct RowView: View {
@State var title: String
@State var framescore: String
@State var totalscore: String
var TotalScore: Int {
let Totalframescore = Int(framescore) ?? 0
return Totalframescore
}
var body: some View {
NavigationLink(destination: TextField("Enter your Frame Score", text: $framescore) .border(Color.black))
{ //Start Frame List Design View
VStack(alignment: .leading) {
HStack(alignment: .center) {
Text(title)
.font(.system(.headline, design: .rounded))
.foregroundColor(Color.blue)
.multilineTextAlignment(.leading)
Divider()
Spacer()
Text("\(framescore)")
.font(.system(.body, design: .rounded))
.multilineTextAlignment(.leading)
Divider()
Spacer()
Text("\(TotalScore)")
.font(.system(.headline, design: .rounded))
.foregroundColor(Color.green)
.multilineTextAlignment(.trailing)
}
}
.listRowBackground(Color.blue)
.frame(height: 60, alignment: .topTrailing)
}
}
}
一种方法可以在每一行上减少到该点为止的分数集。通过遍历分数集,它可以计算帧数、帧分数和先前分数的总和。例如,您的主视图可能如下所示:
struct StartBowlingView: View {
@State var frameScores = [5, 2, 10]
var body: some View {
List{
ForEach(0..<self.frameScores.endIndex) { index in
RowView(title: "\(index + 1)",
framescore: "\(self.frameScores[index])",
totalscore: "\(self.frameScores[0...index].reduce(0, { [=10=] + }))")
}
}
}
}
我意识到保龄球得分可能比简单地对帧求和更复杂(虽然我不知道确切的逻辑),但另一种方法可能是创建一个帧模型来跟踪多个 Int:
struct FrameScore {
let frameNumber: Int
let firstScore: Int
let secondScore: Int
let previousScore: Int
var frameScore: Int { return self.firstScore + self.secondScore }
var totalScore: Int { return self.frameScore + self.previousScore }
var isStrike: Bool { return self.firstScore == 10 }
var isSpare: Bool { return !self.isStrike && self.frameScore == 10 }
}
然后主视图可以更新以保留帧列表:
struct StartBowlingView: View {
@State var frames = [FrameScore]()
var body: some View {
List{
ForEach(self.frames, id: \.frameNumber) { frame in
RowView(title: "\(frame.frameNumber)",
framescore: "\(frame.frameScore)",
totalscore: "\(frame.totalScore)")
}
Button("Add Score") {
let first = Int.random(in: 0...10)
let second = Int.random(in: 0...(10 - first))
// Here's where to add some logic about the prevous frames' strikes and spares affecting the new frame/total score
self.frames.append(
FrameScore(frameNumber: self.frames.count + 1,
firstScore: first,
secondScore: second,
previousScore: self.frames.last?.totalScore ?? 0))
}
}
}
}