Ios 列表是否不异步更新列表中的行数?
我创建了一个@State变量Ios 列表是否不异步更新列表中的行数?,ios,swift,swiftui,swiftui-list,Ios,Swift,Swiftui,Swiftui List,我创建了一个@State变量numberOfPeople,当用户从选择器中选择人数时,它会更新变量,但底部的列表不会更新行数。我做错了什么 List { ForEach (1 ..< numberOfPeople + 2) { num in Text("\(num) People") } } 列表{ ForEach(1..
numberOfPeople
,当用户从选择器中选择人数时,它会更新变量,但底部的列表不会更新行数。我做错了什么
List {
ForEach (1 ..< numberOfPeople + 2) { num in
Text("\(num) People")
}
}
列表{
ForEach(1..
完整代码:
struct ContentView: View {
@State private var checkAmount = ""
@State private var numberOfPeople = 2
@State private var tipPercentage = 2
let tipPercentages : [Int] = [10,15,20,25,0]
var totalPerPerson : Double {
//Calcualte the total per person here
let peopleCount = Double(numberOfPeople + 2)
let tipSelection = Double(tipPercentages[tipPercentage])
let orderAmount = Double(checkAmount) ?? 0
let tipValue = orderAmount / 100 * tipSelection
let grandTotal = orderAmount + tipValue
let amountPerPerson = grandTotal / peopleCount
return amountPerPerson
}
var body: some View {
NavigationView{
Form {
//Amount enter
Section{
TextField("Amount" , text: $checkAmount )
.keyboardType(.decimalPad)
Picker("Number of people" , selection: $numberOfPeople) {
ForEach (2 ..< 100){
Text("\($0) People")
}
}
}
// TIP SELECTION
Section (header : Text("HOW MUCH WILL YOU TIP")) {
Picker("Select tip size" , selection: $tipPercentage){
ForEach (0 ..< tipPercentages.count) {
Text("\(self.tipPercentages[$0]) %")
}
}.pickerStyle(SegmentedPickerStyle())
}
// TOTAL PAY PER PERSON
Section {
Text("£ \(totalPerPerson , specifier: "%.2f / Person")")
}
//Break down of each person amount
List {
ForEach (1 ..< numebrOfPeople + 2){ num in
Text("\(num) People")
}
}
}.navigationBarTitle("SPLITTER")
}
}
}
struct ContentView:View{
@国家私有var checkAmount=“”
@国家私有变量numberOfPeople=2
@国家私有var tipPercentage=2
让TipperContages:[Int]=[10,15,20,25,0]
人均增值税:双倍{
//计算一下这里每人的总数
让peopleCount=Double(numberOfPeople+2)
让tipSelection=Double(tipPercentages[tipPercentages])
订单金额=双倍(支票金额)??0
设tipValue=orderAmount/100*tipSelection
让grandTotal=订单金额+提价值
让amountperperperson=grandTotal/peopleCount
每人返还金额
}
var body:一些观点{
导航视图{
形式{
//金额输入
部分{
文本字段(“金额”,文本:$checkAmount)
.键盘类型(.decimalPad)
选择器(“人数”,选择:$numberOfPeople){
ForEach(2..<100){
文本(“\($0)人”)
}
}
}
//尖端选择
部分(标题:文本(“您将给多少小费”)){
选择器(“选择提示大小”,选择:$tipPercentage){
ForEach(0..
您需要使用不同的ForEach
变体—一个接受动态内容的变体
将id:\.self
添加到代码中的每个ForEach
:
Picker("Number of people", selection: $numberOfPeople) {
ForEach(2 ..< 100, id: \.self) {
Text("\($0) People")
}
}
这样你就不需要使用像1..
这样的黑客了
List {
ForEach(1 ... numberOfPeople, id: \.self) { num in
Text("\(num) People")
}
}