我的选择如何正确出现在我在 SwiftUI 中创建的列表中 [英] How can my selection go right in the list I created in SwiftUI

查看:23
本文介绍了我的选择如何正确出现在我在 SwiftUI 中创建的列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了显示数字数组的列表.当您选择多个数字并点击下面的按钮时,它应该会告诉您所选数字的总和.

I created the list showing the array of digits. When you select multiple digits and hit the button below, it should tell you the sum of the selected digits.

但是,当您在不同的行中选择相同的数字时,它们会同时被检查.我知道这应该通过使用 UUID 来解决,但我想要的最终结果是数字的总和,即 Int.因此,我迷路了......有人可以告诉我如何使它正确吗?

However, when you select the same digits in the different rows, both of them get checked at a time. I understand that this should be fixed by using UUID, but the final result that I want is the sum of the digits, which is Int. Therefore, I have been lost... Can someone tell me how to make it right, please?

此外,即使出于某种原因选择了最后一行,也不会被检查,这太奇怪了.这是显示当前情况的 gif 链接以及下面的完整代码.

Also, the last row doesn't get checked even when selected for some reason, which is so weird. Here is the link to the gif showing the current situation and the entire code below.

import SwiftUI

struct MultipleSelectionList: View {
    
    @State var items: [Int] = [20, 20, 50, 23, 3442, 332]
    @State var selections: [Int] = []
    @State var result: Int = 0

    var body: some View {
        VStack {
            List {
                ForEach(items, id: \.self) { item in
                    MultipleSelectionRow(value: item, isSelected: self.selections.contains(item)) {
                        if self.selections.contains(item) {
                            self.selections.removeAll(where: { $0 == item })
                        }
                        else {
                            self.selections.append(item)
                        }
                    }
                }
            }
            
            Button(action: {
                result = selections.reduce(0, +)
            }, label: {
                Text("Show result")
            })
            .padding()
            
            Text("Result is \(result)")
            Text("The number of items in the array is \(selections.count)")
                .padding()
            Spacer()
        }
        
        
    }
}

struct MultipleSelectionRow: View {
    var value: Int
    var isSelected: Bool
    var action: () -> Void

    var body: some View {
        Button(action: self.action) {
            HStack {
                Text("\(self.value)")
                if self.isSelected {
                    Spacer()
                    Image(systemName: "checkmark")
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        MultipleSelectionList()
    }
}

推荐答案

您可以只存储选择中项目的索引,而不是实际项目.

You could just store the index of the items in selections, instead of the actual items.

你可以这样做:

ForEach(items.indices) { i in
   MultipleSelectionRow(value: items[i], isSelected: self.selections.contains(i)) {
                        if self.selections.contains(i) {
                            self.selections.removeAll(where: { $0 == i })
                        }
                        else {
                            self.selections.append(i)
                        }
                    }

}

我没有运行过,所以可能会有错误,但你懂的.

I have not run this so there may be errors but you get the idea.

这篇关于我的选择如何正确出现在我在 SwiftUI 中创建的列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆