0

I have a graph view in a cell of a TableViewController and I would like to pass an array from the table view controller to that view. How should I tackle the problem?

qlep
  • 15
  • 5

2 Answers2

0

The main way this is accomplished is via the use of the func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { } in which you can set up your individual cell, usually via typecasting after dequeing the cell for index path.

If you're new to iOS development welcome! And I'm sure there was more than one confusing word in post above, but you'll get used to these terms soon!

Check out iOS's example for an example of how this method actually works.

MQLN
  • 2,134
  • 1
  • 14
  • 30
  • OK, maybe I should have been more specific here. Your suggestion helps me to pass the data from the TableViewController to a TableViewCell. Now I need my GraphView, which is of UIView class, to have it. – qlep Mar 24 '21 at 13:08
0

You have to bypass data to cell first then to your graph view. Try this:

class GraphView: UIView {
    var points: [CGPoint]
}

class GraphCell: UITableViewCell {
    @IBOutlet weak var graphView: GraphView!

    func fillData(_ points: [CGPoint]) {
        graphView.point = points
    }
}

And in your controller:

class GraphTableViewController: UIViewController, UITableViewDataSource {
    var points: [CGPoint] = [
        CGPoint.zero,
        CGPoint(x: 100, y: 100)
    ]
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "GraphCell") as? GraphCell else {
            return UITableViewCell()
        }
        cell.fillData(points)
        return cell
    }
}
son
  • 504
  • 3
  • 6
  • Thanks for the outlet hint! I had it as a UIView type in the beginning but changing it to GraphView type allows me to pass data straight from the table view to the graph view property. No need to play around with TableViewDataSource, sections and cell identifiers. – qlep Mar 24 '21 at 14:57