简体   繁体   English

从列表中选择多个项目并在 SwiftUI 中更改它们的颜色

[英]selecting multiple items from the list and changing their color in SwiftUI

I'm looking for a way to solve this problem in a similar way to this.我正在寻找一种与此类似的方法来解决此问题。 Is it possible at all?有可能吗?

For a single item no problem.单品没问题。 the selected item will be in white the rest of my own color "listTextColor".所选项目将为我自己的颜色“listTextColor”的 rest 白色。 However, is it possible in a similar way that the elements selected and saved in the "selectedItems" array are displayed in white and the rest in a different color?但是,是否有可能以类似的方式将“selectedItems”数组中选择和保存的元素显示为白色而 rest 显示为不同的颜色?

example codes:示例代码:

 @State var recordArray: Array = arrayRecords
 @State var selectedItems: Array = []
 @State var selections: String?

List() {
                            ForEach(self.recordArray, id: \.self) { record in
                                Text(record)
                                    .onTapGesture {
                                        self.selections = record
                                        self.selectedItems.append(record)
                                        
                                }
                                .foregroundColor(self.selections == record ? Color(.white) : Color(self.listTextColor2))
                            }
                        }

Your best bet would be to use a View Model instead of plain string and use a flag in it to color the row.您最好的选择是使用 View Model 而不是纯字符串,并在其中使用标志为行着色。

struct ContentView: View {
    @State var recordArray: [RecordItem]
    
    var body: some View {
        List() {
            ForEach(0..<self.recordArray.count, id: \.self) { i in
                VStack {
                    Text(self.recordArray[i].title)
                }
                .border(Color.black)
                .onTapGesture {
                    self.recordArray[i].isSelected.toggle()
                    self.recordArray[i] = self.recordArray[i]
                }
                .foregroundColor(self.recordArray[i].isSelected ? Color(.red) : Color(.green))
            }
        }
    }
}


class RecordItem {
    var title = ""
    var isSelected = false
}

I found answer for this problem.我找到了这个问题的答案。

 @State var recordArray: Array = arrayRecords
 @State var selectedItems: Array = []
 @State var selections: String?

var body: some View {
List() {
                            ForEach(self.recordArray, id: \.self) { record in
                                Text(record)
                                    .onTapGesture {
                                                                                   if self.selectedItems.first(where: { $0 as! String == record } ) != nil {
                                            self.selectedItems.removeAll(where: { $0 as! String == record } )
                                        } else {
                                            self.selectedItems.append(record)
                                        }
                                        
                                }
                                .foregroundColor((self.selectedItems.first(where: { $0 as! String == record } ) != nil) ? Color(.white) : Color(self.listTextColor2))
                            }
                        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM