为什么foreach在Swiftui中不正确更改我的前景颜色?
我正在尝试制作一个简单的应用程序,并且我正在尝试实现一项功能,如果我单击一行,前景颜色将是更改。但是,我有两个问题,首先,当我为核心数据属性切换布尔值时,前端什么也不会发生。该值会更改为真/错误,但前景颜色没有变化。
其次,当我使用简单的@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,我使用另一个帖子的模拟数据提出了另一个问题,这帮助我解决了这个问题。基本上,我只需要制作一个子视图
,这是使用foreach的提取视图。
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
And here is the ExtractedView using ForEach..