NavigationLink in List is automatically designed.
How to set appropriate multi NavigationLink in Cell?
I don't want use ForEach in ScrollView. Because I want to use swipeActions.
struct TimelineView: View {
let tweets: [Tweet] = tweets
let users: [User] = users
var body: some View {
NavigationStack {
List(tweets) { tweet in
let user = users.first { $0.id == tweet.userID }!
NavigationLink(value: tweet) {
HStack {
NavigationLink(value: user) {
Image(systemName: "person")
.foregroundStyle(user.color)
}
VStack {
Text("@\(user.name)")
Text(tweet.text)
}
}
}
.swipeActions(edge: .leading) {
Button("Like") { }
}
}
.navigationDestination(for: Tweet.self) { tweet in
TweetDetailView(tweet: tweet)
}
.navigationDestination(for: User.self) { user in
UserDetailView(user: user)
}
}
}
}
I found resolution. user NavigationPath
struct TimelineView: View {
@State var path = NavigationPath()
let tweets: [Tweet] = tweets
let users: [User] = users
var body: some View {
NavigationStack(path: $path) {
List(tweets) { tweet in
let user = users.first { $0.id == tweet.userID }!
HStack {
Image(systemName: "person")
.onTapGesture {
path.append(user)
}
VStack {
Text("@\(user.name)")
Text(tweet.text)
}
Spacer()
}
.swipeActions(edge: .leading) {
Button("Like") { }
}
.contentShape(Rectangle())
.onTapGesture {
path.append(tweet)
}
}
.navigationDestination(for: Tweet.self) { tweet in
TweetDetailView(tweet: tweet)
}
.navigationDestination(for: User.self) { user in
UserDetailView(user: user)
}
}
}
}