Swiftui 导航 link 无法正确更新网格视图中每个用户的页面

Swiftui Navigation link not updating correctly with each individual user's page in a gridview

我正在尝试在 Vgrid 中实现与用户的视图,当点击用户的个人资料图片时,它应该直接转到 his/her 个人资料。我已经尝试了很多方法让它工作需要任何帮助。

当我点击任何用户时,它会转到我的 firebase 集合中的第一个用户,并且不会导航到我点击的用户的个人资料。

VGrid 中的 PeepsGridView() 工作正常,用户搜索并正确匹配它只是点击每个用户的正确配置文件的问题。

下面是带有 Vgrid 的视图和 link 到配置文件视图的导航:

struct PeopleView: View {
    @ObservedObject var peoplesViewModel = PeopleViewModel()
    @EnvironmentObject var profileviewModel: ProfileViewModel
    private let items = [GridItem(), GridItem(), GridItem()]
    
    var body: some View {
        NavigationView {
                VStack(spacing: 0) {
                    PeopleTopView()
                        .padding(.top, 45)
                    
                    PeopleSearchBarView(text: $peoplesViewModel.searchText)
                        .padding()
                        
                   ScrollView(.vertical, showsIndicators: false) {
                       
                                NavigationLink {
                                    ScrollView(.vertical, showsIndicators: false) {
                                        ForEach(self.peoplesViewModel.users) { susers in
                                            VStack {
                                              ProfileInfoView(user: susers)
                                            }
                                        }
                                    }
                                } label: {
                                    LazyVGrid(columns: items, spacing: 2) {
                                        ForEach(peoplesViewModel.searchableUsers) { peep in
                                    PeepsGridView(userInfo: peep)
                                                
                                            
                                        }
                                    }
                                }
                  } //End ScrollView
                }
                .navigationBarTitle("")
                .navigationBarHidden(true)
                .ignoresSafeArea(.all, edges: .top)
        } //End NavigationView
    }
}

我正在从 Firebase 获取我的用户,所以我将包括我 linked:

的 2 个视图模型
import Foundation

class PeopleViewModel: ObservableObject {
    @Published var users = [User]()
    @Published var searchText = ""
    
    var searchableUsers: [User] {
        if searchText.isEmpty {
           return users
        } else {
            let lowercasedQuery = searchText.lowercased()
            
            return users.filter ({
                [=12=].username.contains(lowercasedQuery) ||
                [=12=].fullname.lowercased().contains(lowercasedQuery)
            })
        }
    }
     
    
    let service = UserService()
    
    init() {
        fetchUsers()
    }
    
    func fetchUsers() {
        service.fetchUsers { users in
            self.users = users
        }
    }
}

这是第二个视图模型:

import Foundation

class ProfileViewModel: ObservableObject {
    @Published var feedtry = [FeedTry]()
    @Published var likedTry = [FeedTry]()
    private let service = UploadTryService()
    private let userService = UserService()
    var user: User
    
    
    init (user: User) {
        self.user = user
        self.fetchUserFeedTry()
        self.fetchLikedTry()
    }
    
    var actionButtonTitle: String {
        return user.isCurrentUser ? "Edit Profile" : "Follow"
    }
    

    
    func feedtrys(forFilter filter: ProfileFilterViewModel) -> [FeedTry] {
        switch filter {
        case .music:
            return feedtry
        case .pictures:
            return feedtry
        case .videos:
            return likedTry
        }
    }
    
    
    func fetchUserFeedTry() {
        guard let uid = user.id else { return }
        service.fetchTry(forUid: uid) { feedtrys in
            self.feedtry = feedtrys
            
            for i in 0 ..< feedtrys.count {
                self.feedtry[i].user = self.user
            }
        }
    }
    
    func fetchLikedTry() {
        guard let uid = user.id else { return }
        
        service.fetchLikedTry(forUid: uid) { feedtry in
            self.likedTry = feedtry
            
            for i in 0 ..< feedtry.count {
                let uid = feedtry[i].uid
                
                self.userService.fetchUser(withUid: uid) { user in
                    self.likedTry[i].user = user
                }
            }
        }
    }
    
    
}

最后,这是我个人资料视图正文上方的代码:

import SwiftUI
import SDWebImageSwiftUI

struct ProfileView: View {
    @ObservedObject var profileviewModel: ProfileViewModel
    @Namespace var animation
    
    
    init(user: User) {
        self.profileviewModel = ProfileViewModel(user: user)
    }
    
}

你不可能拥有一个包含所有可能性的NavigationLink。相反,你创造了所有的可能性,然后在每一种可能性周围加上 NavigationLink。您的代码的另一个问题是您使用了两组不同的数据,一组作为 NavigationLink 的数据模型,另一组作为标签的数据模型。这些必须是相同的,否则事情就会无可救药地混淆。此外,如果您的 suserspeep 数量不总是相同,则当一个先于另一个用完时,您将崩溃。由于您想使用 susers 进行导航,我在下面的代码中使用了该变量:

ScrollView(.vertical, showsIndicators: false) {
    LazyVGrid(columns: items, spacing: 2) {
        ForEach(self.peoplesViewModel.users) { susers in           
            NavigationLink {
                ProfileInfoView(user: susers)
            } label: {
                PeepsGridView(userInfo: users)
            }
        }
    }
} //End ScrollView