1

I want to access some cell values outside of 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)

    }

I want to access cell.Books, cell.Title and cell.Price in a button function. However, with my solution above, I want to be able to capture any row in my tableViewCell and not just (row: 0, section: 0) but I am not sure how to do that.

juelizabeth
  • 485
  • 1
  • 8
  • 31
  • 2
    Don't try to access cell information. It's a view, not your data. Access your data from your data model. – rmaddy Jun 17 '17 at 19:57
  • @rmaddy huh? How do I do that? – juelizabeth Jun 17 '17 at 20:15
  • 1
    Access the same data model you use in all of the table view dataSource methods such as `numberOfRowsInSection` and `cellForRowAt`. – rmaddy Jun 17 '17 at 20:16
  • Just do as rmaddy has suggested. Though I'm really not sure what your question is. You know how to access it row:0, section:0...you can access any other indexPath like that e.g. row:10, section:2... – mfaani Jun 17 '17 at 20:27
  • @Honey but I want the user be able to click on any cell they want and by me hard coding the row and section, any user will not be able to click on what ever cell they want – juelizabeth Jun 17 '17 at 20:29
  • Then don't hard-code the row and section. – NRitH Jun 17 '17 at 22:13

1 Answers1

0

In the cellforRowAtIndexPath, update the tag of button to the indexpath.row, And in the button action method retrieve the data from the model based on the tag..

Arthi
  • 128
  • 1
  • 7