为什么foreach在Swiftui中不正确更改我的前景颜色?

发布于 2025-01-25 04:48:46 字数 2190 浏览 3 评论 0原文

我正在尝试制作一个简单的应用程序,并且我正在尝试实现一项功能,如果我单击一行,前景颜色将是更改。但是,我有两个问题,首先,当我为核心数据属性切换布尔值时,前端什么也不会发生。该值会更改为真/错误,但前景颜色没有变化。

其次,当我使用简单的@State布尔值时,所有列表行前景颜色都会改变,而不仅仅是我敲击的行。

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)))
                        
                    }

因此,使用核心数据变量item.done.toggle在切换时无助于更改前景颜色,但它确实在true/false之间切换。另外,使用@State var testbool更改所有行,而不仅仅是敲击。

我想点击一排,然后仅在该行上更改颜色。

我制作了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())
            }
        }
    }

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. However, I have two problems, first when I toggle the boolean value in for my core data attribute, nothing happens on the front end. The value changes true/false but the foreground color is not changing.

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.

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. Alternatively, using the @State var testBool changes ALL the rows instead of just the one tapped.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女 2025-02-01 04:48:46

因此,我使用另一个帖子的模拟数据提出了另一个问题,这帮助我解决了这个问题。基本上,我只需要制作一个子视图

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)))
    }
}

,这是使用foreach的提取视图。

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)

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..

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文