我正在学习使用 SwiftUI 进行 iOS 开发。我使用 NavigationStack 将用户传递给 DetailView:
let shape = RoundedRectangle(cornerRadius: Constants.cornerRadius)
NavigationStack {
ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: Constants.gridItemSize))]) {
ForEach(userWrappers) { userWrapper in
Section(userWrapper.section) {
ForEach(userWrapper.users, id: \.self.login) { user in
NavigationLink(value: user) {
VStack {
AsyncImage(url: URL(string: user.avatarUrl)) { image in
image
.resizable()
.scaledToFit()
.frame(width: Constants.frameSize, height: Constants.frameSize)
.clipShape(shape)
} placeholder: {
shape.foregroundColor(.secondary)
.frame(width: Constants.frameSize, height: Constants.frameSize)
}
Text(user.login)
.font(.title3)
}.padding()
}
}
}
}
}.navigationDestination(for: GithubUser.self) { user in
DetailView(user: user)
}
.navigationTitle("List")
}
}
}
如你所见,我将用户传递给 DetailView。它显示用户名和头像,但无法在 DetailView 中始终显示个人资料,并且不显示个人资料,如下面的代码所示:
VStack {
AsyncImage(url: URL(string: user.avatarUrl)) { image in
image
.resizable()
.scaledToFit()
} placeholder: {
Rectangle()
.foregroundColor(.secondary)
.frame(width: Constants.frameSize, height: Constants.frameSize)
}.padding()
Text(user.bio ?? "No Bio")
.padding()
Spacer()
}
.navigationTitle(user.login)
这是我的模型类:
struct GithubUser: Codable, Hashable {
let login: String
let avatarUrl: String
let bio: String?
}
您可以在https://github.com/alirezaeiii/Networking-Assignment找到源代码
你能帮助我吗?