【问题标题】:Capturing cell values outside of cellForRowAt [duplicate]在 cellForRowAt 之外捕获单元格值
【发布时间】:2017-11-20 09:16:30
【问题描述】:

我想访问 cellForRowAt 之外的一些单元格值。

  @IBAction func ShareClicked(_ sender: UIButton) -> Void {
        print("Hello Edit Button")

        let indexPath = IndexPath(row: 0, section: 0)
        let cell = TableView.cellForRow(at: indexPath) as! ProfileTableViewCell

        let activityVC = UIActivityViewController(activityItems: [cell.Books!, "I found this book", cell.Title!,"in the LitSwap app for :",cell.Price! ], applicationActivities: nil)
        activityVC.popoverPresentationController?.sourceView = self.view

        self.present(activityVC, animated: true, completion: nil)

    }

我想在按钮功能中访问 c​​ell.Books、cell.Title 和 cell.Price。但是,通过上面的解决方案,我希望能够捕获 tableViewCell 中的任何行,而不仅仅是 (row: 0, section: 0),但我不知道该怎么做。

【问题讨论】:

  • 不要尝试访问单元格信息。这是一个视图,而不是您的数据。从您的数据模型访问您的数据。
  • @rmaddy 嗯?我该怎么做?
  • 访问您在所有表视图数据源方法中使用的相同数据模型,例如numberOfRowsInSectioncellForRowAt
  • 按照 rmaddy 的建议去做。虽然我真的不确定你的问题是什么。您知道如何访问它 row:0, section:0 ...您可以访问任何其他 indexPath ,例如行:10,部分:2...
  • @Honey 但我希望用户能够点击他们想要的任何单元格,并且通过我对行和部分进行硬编码,任何用户都无法点击他们想要的任何单元格跨度>

标签: ios swift uitableview firebase-realtime-database


【解决方案1】:

在cellforRowAtIndexPath中,将button的tag更新为indexpath.row, 并在按钮操作方法中根据标签从模型中检索数据..

【讨论】:

  • 不,不要使用标签。如果表格支持添加、删除或移动行,它们将是错误的。
  • @rmaddy 那么我们应该使用什么 istead?
  • @ArgaPK 单击问题顶部的链接副本并查看答案。
猜你喜欢
  • 1970-01-01
  • 2015-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多