导航到详细信息查看按钮单击列表行(tableViewCell)在swiftui中

发布于 2025-02-07 22:49:54 字数 187 浏览 2 评论 0 原文

我有带有单元格的列表/表观视图。 每个单元格都包含有关给定项目的详细信息和一些按钮,例如编辑项目,显示项目的详细信息,显示给定项目等的子视图等。

现在, >,但是现在,如果有人单击该单元格上的任何地方,它将移至详细屏幕。

我不想在屏幕上的任何地方单击任何地方,我只想在有人单击该单元格中的“显示详细信息”按钮时才按详细信息。

I have List/TableView with Cells. Now each cell contains detail about given Item and some buttons like Edit the Item, Show Detail of Item, Show subview of given Item etc.

Now, for case of showing detail of Item, I have tried to add NavigationLink, but now if anyone clicks anywhere on that cell, it moves to detail screen.

I dont want to move to detail screen on clicking anywhere in screen, I want to push detail screen only when someone clicks "Show Detail" button in that cell.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似梦非梦 2025-02-14 22:49:54
  1. 使用 .hidden() navigationLink上修改器以防止列表项目捕获其动作

  2. 更改 .buttonStyle >自动 to 列表项目上的其他东西以防止其捕获按钮操作。 (例如无边界

工作演示

struct ContentView: View {
@State var selectedTag: Int?

var body: some View {
NavigationView {
List(1...10, id: \.self) { id in
HStack {
Text("Item \(id: id)")
Spacer()

Button("Show detail") { selectedTag = id }
.background(link(id))
}.buttonStyle(.borderless) ///

  1. Use the .hidden() modifier on the navigationLink to prevent the list item from capturing it's action

  2. Change the .buttonStyle from automatic to something else on the list item to prevent it from capturing the button action. (for example borderless)

Working Demo

struct ContentView: View {
    @State var selectedTag: Int?

    var body: some View {
        NavigationView {
            List(1...10, id: \.self) { id in
                HStack {
                    Text("Item \(id: id)")
                    Spacer()
                    
                    Button("Show detail") { selectedTag = id }
                    .background(link(id))
                }.buttonStyle(.borderless) /// ???? ⚠️ on the item! **NOT** on the button!!!
            }
        }
    }
    
    func link(id: Int) -> some View {
        NavigationLink("",
            destination: Text("\(id) Selected"),
            tag: id,
            selection: $selectedTag
        ).hidden()
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文