2

I have been trying to develop a view controller with multiple charts(bar chart, line chart, pie chart). I created a table view and custom table view cell. There is a UIView inside custom table view cell. However, when I am trying to cast that UIView to BarchartView it gives me an error

Could not cast value of type 'UIView' (0x10a8e7f40) to 'Charts.LineChartView' (0x1074f63a0).

How can I achieve that multiple charts in same table view? Thanks in advance.

cellForRowAt indexPath:

let cell = myTableView.dequeueReusableCell(withIdentifier: "chart") as? GraphicCell 
var lineChart:LineChartView = cell?.chart as! LineChartView
lineChart.noDataText = "A" 
return cell! 

the view outlet that I have created in GraphicCell is UIView type

The charts which are shown depends on the user choice. User can select one bar chart and two line chart or only two line chart without bar chart. I do not understand completely how to achieve this. I have added my project to GitHub project link

DevB2F
  • 4,674
  • 4
  • 36
  • 60
atalayasa
  • 3,310
  • 25
  • 42
  • Can you share the code? – DevB2F Dec 14 '17 at 18:03
  • In my cellForRowAt method there is let cell = myTableView.dequeueReusableCell(withIdentifier: "chart") as? GraphicCell var lineChart:LineChartView = cell?.chart as! LineChartView lineChart.noDataText = "A" return cell! the view outlet that I have created in GraphicCell is UIView type – atalayasa Dec 14 '17 at 18:09

2 Answers2

1

The chartView can´t be of type UIView, it has to have the correct type when you declare it. You can have 3 different views inside the tableView cell, like this:

@IBOutlet weak var barChart: BarChartView!
@IBOutlet weak var pieChart: PieChartView!
@IBOutlet weak var lineChart: LineChartView!

and then use the one you need, depending on which graph type you want. If you are using Storyboard, you also need to choose class for each view as BarChartView, LineChartView or PieChartView in the Storyboard.

DevB2F
  • 4,674
  • 4
  • 36
  • 60
1

You need to create prototype cells for each of the types of charts, which you want to use in your TableView. In each prototype cell you need to put UIView and then change the class of UIView to LineChartView, BarChartView, etc.

Set class for view

Also you need to define your own class for each prototype cell, e.g:

class MyLineChartCell: UITableViewCell {

    @IBOutlet weak var lineChartView: LineChartView!

    func configure (/* ... */) {
        lineChartView.noDataText = "You have no data"
        // ...
    }

} 

.

Use this classes for you prototype cells: Set class for cell

Then in func func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell you could choose which prototype will be used at the moment.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyLineChartCellIdentifier") as! MyLineChartCell
        cell.configure(/* Data, Colors, etc. */)
    }
    if indexPath.row == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyBarChartCellIdentifier") as! MyBarChartCell
        cell.configure(/* Data, Colors, etc. */)
    }
    //... some other types of cells

    return cell
}
AlexSmet
  • 2,141
  • 1
  • 13
  • 18
  • Thanks for answer I tried to do it in that way but what if I do not have line chart, there are only bar chart and pie chart ? – atalayasa Dec 15 '17 at 06:17
  • Of course you can use any type of charts which need for you. I have used LineChartView only for example, you can replace LineChartView to PieChartView. – AlexSmet Dec 15 '17 at 08:27
  • But the charts which are shown depends on the user choice. User can select one bar chart and two line chart or only two line chart without bar chart. I do not understand completely how to achieve this. I have added my project to GitHub could you please take a look? I really stucked https://github.com/melihdolgun/MultipleCharts/tree/master/ChartsDeneme[link] – atalayasa Dec 15 '17 at 08:56
  • In your question you wrote that you had created a TableView with custom cell. I decided then you have need to show different type of charts in different cells. But now you have written that user can select which type of chart need to display. So, do you need a TableView with list of different charts or you need a single View and a switch for changing type of displaying chart? – AlexSmet Dec 15 '17 at 12:39
  • Actually I need single view and switch for type of displaying chart but I think it is more complex so I have added 3 view into custom cell and according to chart that I need to display I am changing it. Your answer helped me a lot thank you very much. – atalayasa Dec 16 '17 at 22:00