SwiftUI – 如何在列表中另一个选定的对象为真时将对象状态默认为假

huangapple go评论84阅读模式
英文:

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

SwiftUI – 如何在列表中另一个选定的对象为真时将对象状态默认为假
SwiftUI – 如何在列表中另一个选定的对象为真时将对象状态默认为假

英文:

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

SwiftUI – 如何在列表中另一个选定的对象为真时将对象状态默认为假
SwiftUI – 如何在列表中另一个选定的对象为真时将对象状态默认为假

答案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()
        }
    }
}

huangapple
  • 本文由 发表于 2023年8月4日 21:09:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定