简体   繁体   English

为什么 ForEach 没有在 SwiftUI 中正确更改我的前景色?

[英]Why is ForEach not changing my Foreground color correctly in SwiftUI?

I am trying to make a simple Todo app and I'm trying to implement a feature where if I click on a row, the foreground color will be change.我正在尝试制作一个简单的 Todo 应用程序,并且正在尝试实现一项功能,如果我点击一行,前景色将会改变。 However, I have two problems, first when I toggle the boolean value in for my core data attribute, nothing happens on the front end.但是,我有两个问题,首先,当我为我的核心数据属性切换 boolean 值时,前端没有任何反应。 The value changes true/false but the foreground color is not changing.该值更改为 true/false,但前景色未更改。

Second, when I instead use a simple @State boolean value, ALL the list row foreground colors change instead of just the row I tapped on.其次,当我改为使用简单的 @State boolean 值时,所有列表行前景 colors 发生变化,而不仅仅是我点击的行。

import SwiftUI
import ChameleonFramework

struct ItemView: View {
    
    @Environment(\.managedObjectContext) private var moc
    @StateObject var category: Category
    @State private var showAddItemView = false
    @State private var text = ""
    @State var testBool = false
    var body: some View {
        NavigationView {
            VStack {
                
                List {
                    ForEach(searchResults) { item in
                        
                        Text(item.unwrappedTitle)
                            .foregroundColor(item.unwrappedDone ? .red : .blue)
//                            .foregroundColor(Color(ContrastColorOf(backgroundColor: UIColor(hexString: category.unwrappedColor), returnFlat: true)))
                            .onTapGesture {
                                print(item.unwrappedTitle)
                                item.done.toggle()
                                print(item.done)
                                try? moc.save()
                            }
                            .listRowBackground(Color(UIColor(hexString: category.unwrappedColor)))
                        //                            .listRowBackground(Color(UIColor(hexString: category.unwrappedColor).darken(byPercentage: 0.5)))
                        //                            .listRowBackground(Color(HexColor(hexString: category.unwrappedColor)))
                        
                    }

So using the Core Data variable item.done.toggle does nothing to change the foreground color when toggled but it does switch between true/false.因此,使用 Core Data 变量item.done.toggle不会在切换时更改前景色,但它会在 true/false 之间切换。 Alternatively, using the @State var testBool changes ALL the rows instead of just the one tapped.或者,使用@State var testBool更改所有行,而不仅仅是被点击的行。

I want to tap on a row and have it change color on only that row.我想点击一行,让它只改变那一行的颜色。

I made searchResults equal to category.itemArray for purposes of search and filtering.出于搜索和过滤的目的,我将searchResults设置为等于category.itemArray

    var searchResults: [Item] {
        if text.isEmpty {
            return category.itemArray
        } else {
            return category.itemArray.filter { Item in
                Item.title!.lowercased().contains(text.lowercased())
            }
        }
    }

So I asked another question using mock data on another post and that helped me solve this.所以我在另一篇文章中使用模拟数据问了另一个问题,这帮助我解决了这个问题。 Basically, I just needed to make a subview基本上,我只需要制作一个子视图

struct ExtractedView: View {
    @Environment(\.managedObjectContext) private var moc
    @State var testBool = false
    
    let item: Item
    
    var body: some View {
        Text(item.unwrappedTitle)
            .foregroundColor(testBool ? .red : .green)
            .foregroundColor(Color(ContrastColorOf(backgroundColor: UIColor(hexString: item.unwrappedColor), returnFlat: true)))
            .onTapGesture {
                print(item.unwrappedTitle)
                item.done.toggle()
                print(item.unwrappedDone)
                testBool.toggle()
                try? moc.save()
            }
            .listRowBackground(Color(UIColor(hexString: item.unwrappedColor)))
    }
}

And here is the ExtractedView using ForEach..这是使用 ForEach 的 ExtractedView ..

import SwiftUI
import ChameleonFramework

struct ItemView: View {
    
    @Environment(\.managedObjectContext) private var moc
    @StateObject var category: Category
    @State private var showAddItemView = false
    @State private var text = ""
//    @State var testBool = false
    var body: some View {
        NavigationView {
            VStack {
                
                List {
                    ForEach(searchResults) { item in
                        
                        ExtractedView(item: item)
                        //                            .listRowBackground(Color(UIColor(hexString: category.unwrappedColor).darken(byPercentage: 0.5)))
                        //                            .listRowBackground(Color(HexColor(hexString: category.unwrappedColor)))
                        
                    }
                    .onDelete(perform: deleteItem)

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

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