List(list) { feature in
NavigationButton(destination: destination)) {
ListCell(feature: feature)
}
}
我想通过更改目标 View 而不是每行的单个 View 来在点击行时导航到不同的 View
有什么办法吗?
最佳答案
是的,有办法。
这是具有完全动态“单元格”的主体。 tableRow
函数呈现单元格的内容,而 destination
函数计算 NavigationButton
的目标。
var body: some View {
List {
// Renders the table with all the active conversations
ForEach(appData.currentUser.conversations) { conversation in
NavigationButton(destination: self.destination(for: conversation) ) {
self.tableRow(for: conversation)
}
}
}
}
destination
函数非常简单,这里唯一的“技巧”是使用 AnyView
作为返回类型。
private func destination (for conversation: Conversation) -> AnyView {
if conversation.mode == .personal {
return AnyView(ConversationDetail(conversation: conversation))
} else if conversation.mode == .groupChat {
return AnyView(ConversationDetailGroup(conversation: conversation))
} else {
return AnyView(ConversationDetailJoin(conversation: conversation))
}
}
关于ios - SwiftUI 导航按钮 : How to navigate to different destinations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56819147/