同一视图中的多个图表IOS图表

问题描述 投票:2回答:2

我一直在尝试开发一个带有多个图表的视图控制器(条形图,折线图,饼图)。我创建了一个表视图和自定义表视图单元格。自定义表格视图单元格中有一个UIView。但是,当我试图将UIView投射到BarchartView时,它给了我一个错误

无法将'UIView'(0x10a8e7f40)类型的值转换为'Charts.LineChartView'(0x1074f63a0)。

如何在同一个表视图中实现多个图表?提前致谢。

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

显示的图表取决于用户的选择。用户可以选择一个条形图和两个折线图,也可以只选择两个没有条形图的折线图。我完全不明白如何实现这一目标。我已将我的项目添加到GitHub project link

ios swift uitableview charts ios-charts
2个回答
1
投票

您需要为要在TableView中使用的每种类型的图表创建原型单元格。在每个原型单元格中,您需要放置UIView,然后将UIView的类更改为LineChartView,BarChartView等。

Set class for view

您还需要为每个原型单元定义自己的类,例如:

class MyLineChartCell: UITableViewCell {

    @IBOutlet weak var lineChartView: LineChartView!

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

} 

.

使用此类为您原型单元格:Set class for cell

然后在func func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell中你可以选择目前使用的原型。

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
}

1
投票

chartView不能是UIView类型,它在声明时必须具有正确的类型。 tableView单元格中可以有3个不同的视图,如下所示:

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

然后使用您需要的那个,具体取决于您想要的图表类型。如果您使用的是Storyboard,则还需要为Storyboard中的每个视图选择类作为BarChartView,LineChartView或PieChartView。

© www.soinside.com 2019 - 2024. All rights reserved.