英文:
SwiftUI - How to default the object state back to false when another object which is selected is true inside list
问题
我正在尝试更新isFavoriteToggle()
函数,以便在我选择列表中的另一个对象时,之前状态为true的isFavorite对象状态会恢复为false,并且此对象的isFavorite状态会更改为true。
例如,当我从列表中选择"item 1"时,isFavorite布尔对象状态会更改为true。
如果我在列表中选择"item 3",则"item 1"的isFavorite状态会恢复为false,"item 3"会变为true。
基本上,我希望列表中的一项每次都处于isFavorite状态为true,而所有其他项都为false。
import SwiftUI
struct Items: Identifiable {
let id = UUID().uuidString
let name: String
var isFavorite: Bool = false
static var samples: [Items] = [
Items(name: "item 1"),
Items(name: "item 2"),
Items(name: "item 3"),
Items(name: "item 4"),
Items(name: "item 5"),
Items(name: "item 6"),
Items(name: "item 7"),
]
}
import SwiftUI
struct ExampleList: View {
@State var queryList: [Items] = Items.samples
func isFavoriteToggle(element: Items) {
guard let index = queryList.firstIndex(where: {$0.id == element.id}) else {return}
queryList[index].isFavorite.toggle()
}
var body: some View {
NavigationStack {
List {
ForEach($queryList) { $list in
Button {
isFavoriteToggle(element: list)
} label: {
HStack {
Text(list.name)
Spacer()
Image(systemName: "star.fill")
.foregroundColor(list.isFavorite ? Color.yellow : Color.gray)
}
}
}
}
.navigationTitle("Test")
.listStyle(.plain)
.padding()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ExampleList()
}
}
感谢您的支持。谢谢!
英文:
I am trying to update the function(isFavoriteToggle()) that when I select another object in the list the object which isFavorite state changed earlier to true get default back to false and this object isFavorite state to change to true.
For example when I select "item 1" from the list the isFavorite Boolean object state change to true.
If I select "item 3" in the list then "item 1" isFavorite state change back to false and "item 3" to true.
Basically I want one Item at a time in the list to be isFavorite state to true and all other is false.
import SwiftUI
struct Items: Identifiable {
let id = UUID().uuidString
let name: String
var isFavorite: Bool = false
static var samples: [Items] = [
Items(name: "item 1"),
Items(name: "item 2"),
Items(name: "item 3"),
Items(name: "item 4"),
Items(name: "item 5"),
Items(name: "item 6"),
Items(name: "item 7"),
]
}
import SwiftUI
struct ExampleList: View {
@State var queryList: [Items] = Items.samples
func isFavoriteToggle(element: Items) {
guard let index = queryList.firstIndex(where: {$0.id == element.id}) else {return}
queryList[index].isFavorite.toggle()
}
var body: some View {
NavigationStack {
List {
ForEach($queryList) { $list in
Button {
isFavoriteToggle(element: list)
} label: {
HStack {
Text(list.name)
Spacer()
Image(systemName: "star.fill")
.foregroundColor(list.isFavorite ? Color.yellow : Color.gray)
}
}
}
}
.navigationTitle("Test")
.listStyle(.plain)
.padding()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ExampleList()
}
}
Appreciate your support. Thanks
答案1
得分: 1
你应该尝试这个
struct ExampleList: View {
@State var queryList: [Items] = Items.samples
func isFavoriteToggle(element: Items) {
queryList.indices.forEach { index in
queryList[index].isFavorite = false
}
guard let index = queryList.firstIndex(where: { $0.id == element.id }) else { return }
queryList[index].isFavorite = true
}
var body: some View {
NavigationStack {
List {
ForEach($queryList) { $list in
Button {
isFavoriteToggle(element: list)
} label: {
HStack {
Text(list.name)
Spacer()
Image(systemName: "star.fill")
.foregroundColor(list.isFavorite ? Color.yellow : Color.gray)
}
}
}
}
.navigationTitle("Test")
.listStyle(.plain)
.padding()
}
}
}
英文:
You should try this
struct ExampleList: View {
@State var queryList: [Items] = Items.samples
func isFavoriteToggle(element: Items) {
queryList.indices.forEach { index in
queryList[index].isFavorite = false
}
guard let index = queryList.firstIndex(where: { $0.id == element.id }) else { return }
queryList[index].isFavorite = true
}
var body: some View {
NavigationStack {
List {
ForEach($queryList) { $list in
Button {
isFavoriteToggle(element: list)
} label: {
HStack {
Text(list.name)
Spacer()
Image(systemName: "star.fill")
.foregroundColor(list.isFavorite ? Color.yellow : Color.gray)
}
}
}
}
.navigationTitle("Test")
.listStyle(.plain)
.padding()
}
}
}
答案2
得分: 1
If a list can have one and only one favourite, then this feels like a single property to represent which item is the favourite, rather than a collection of interdependent boolean values.
Assuming that no item in the list is a favourite to begin with, you could go for something like:
struct ExampleList: View {
@State var queryList: [Items] = Items.samples
@State var favorite: Items?
var body: some View {
NavigationStack {
List {
ForEach($queryList) { $list in
Button {
favorite = list
} label: {
HStack {
Text(list.name)
Spacer()
Image(systemName: "star.fill")
.foregroundColor(list.id == favorite?.id ? Color.yellow : Color.gray)
}
}
}
}
.navigationTitle("Test")
.listStyle(.plain)
.padding()
}
}
}
英文:
If a list can have one and only one favourite, then this feels like a single property to represent which item is the favourite, rather than a collection of interdependent boolean values.
Assuming that no item in the list is a favourite to begin with, you could go for something like:
struct ExampleList: View {
@State var queryList: [Items] = Items.samples
@State var favorite: Items?
var body: some View {
NavigationStack {
List {
ForEach($queryList) { $list in
Button {
favorite = list
} label: {
HStack {
Text(list.name)
Spacer()
Image(systemName: "star.fill")
.foregroundColor(list.id == favorite?.id ? Color.yellow : Color.gray)
}
}
}
}
.navigationTitle("Test")
.listStyle(.plain)
.padding()
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论