目前,因为它在ForEach循环中,并且我只有一个状态变量,所以当我按下Follow时,它们都会更改为Follow.如果isFollowing被切换,我使用了一个三元操作符来切换按钮的 colored颜色 和文本.当按钮被点击时,我该如何让它只为指定的用户切换.我只需要在循环外做3个按钮吗?当我在按钮中使用user.isFollowingUser.切换时,它告诉我不能Mutations 用户.

import SwiftUI
struct InstagramModel: Identifiable{
    let id: String = UUID().uuidString
    let username: String
    let userImage: String
    let followers: Int
    let isVerified: Bool
    let isFollowingUser: Bool
}
struct ModelPractice: View {
    
    @State private var users: [InstagramModel] = [
        InstagramModel(username: "aleedagenie", userImage: "AliImage", followers: 490, isVerified: true, isFollowingUser: false),
        InstagramModel(username: "nicole29", userImage: "volcano2", followers: 1090, isVerified: true, isFollowingUser: false),
        InstagramModel(username: "shamat81", userImage: "crashedCar", followers: 290, isVerified: false, isFollowingUser: false)
    ]
    @State private var isFollowing: Bool = false
    @State private var isShowDialog: Bool = false
    @State private var background: Color = .mint
    var body: some View {
        
        NavigationView{
            VStack{
            List{
                Section {
                    ForEach(users) { user in
                        
                        VStack(alignment: .leading) {
                            HStack {
                                Image(user.userImage)
                                    .resizable()
                                    .frame(width: 35, height: 35)
                                    .clipShape(Circle())
                                
                                VStack(alignment: .leading) {
                                    Text(user.username)
                                        .font(.headline)
                                    HStack {
                                        Text("Followers:")
                                            .font(.caption)
                                        Text("\(user.followers)")
                                            .font(.caption)
                                    }
                                }
                                Spacer()
                                if user.isVerified{
                                    Image(systemName: "checkmark.seal.fill")
                                        .foregroundColor(.blue)
                                }
                                
                            }
                            Button {
                                isFollowing.toggle()
                            } label: {
                                Text(isFollowing ? "Following" : "Follow")
                                    .foregroundColor(isFollowing ? .black: .white)
                                    .frame(maxWidth: 90)
                                    .background(isFollowing ? .white: .blue)
                                    .cornerRadius(12)
                            }
                            .padding(.horizontal, 44)
                        }
                    }
                } header: {
                    Text("Instagram Users")
                }
                .listRowBackground(background)
            }
                Button {
                    isShowDialog.toggle()
                } label: {
                    Text("Change Page Style")
                        .bold()
                        .frame(maxWidth: 140)
                        
                        .background(.orange)
                        .cornerRadius(20)
                }
                .confirmationDialog("Text", isPresented: $isShowDialog, actions: {
                    Button {
                        background = .yellow
                    } label: {
                        Text("Option 1")
                    }
                    Button {
                        background = .gray
                    } label: {
                        Text("Option 2")
                    }
                    Button {
                        background = .green
                    } label: {
                        Text("Option 3")
                    }
                })
            .navigationTitle("Instagram")
                
                
                
            }
            
        }
    }
}

struct ModelPractice_Previews: PreviewProvider {
    static var previews: some View {
        ModelPractice()
    }
}

推荐答案

这里的问题是,你正在试图Mutations closure中的variable,称为user.user是一个临时变量,与你的users无关,所以你不能改变它.

相反,你应该改变users.

这是我的演示,试试看.代码如下图:

enter image description here

struct UserModel: Identifiable {
  var id = UUID()
  var name: String = ""
  var following = false
}
struct DemoView: View {
  @State var listUser = [
    UserModel(name: "Lamb Chop", following: false),
    UserModel(name: "Steak", following: false)
]
  var body: some View {
    List {
        ForEach(listUser.indices) { index in
            HStack {
                Text(listUser[index].name)
                Button {
                    listUser[index].following.toggle()
                } label: {
                    Text(listUser[index].following ? "following" : "follow")
                }
                .padding(5)
                .background(.black)
                .cornerRadius(15)
            }
        }
    }
  }
}

Swift相关问答推荐

是否有一个Kotlin等价的Swift s @ AddendableReport注释'

如何禁用MacOS上SwiftUI中按钮周围的聚焦环(或高亮显示)?

如何异步返回视图?什么?

如何在macOS中延迟退出应用程序的退出操作?

为什么这个快速代码不显示文本字段?

SwiftUI 交易:根据外部状态制作一个动画或静止的按钮

为什么我的 tableView 没有推送到 tableView 内的 detailViewController?

避免 `(())` 显式指定 Void 类型的枚举关联值

Swift 并发任务与调度队列线程:是什么决定同时运行多少任务?

在 RealmSwift 中表示范围值?

如何将视图添加到多行文本视图的末尾?

需要将 json 映射到 Swift 中的模型

快速更改tableView中的数据

如何更新 UserDefault 中的特定值

使 Picker 与其他 BinaryInteger 类型兼容

Swift Swiftui - 将 colored颜色 保存到 UserDefaults 并从 @AppStorage 使用它

如何获得不同的插入和移除过渡动画?

找不到接受提供的参数的/的重载

UILabel 文本不换行

Swift 2.0 按属性对对象数组进行排序