我一直在尝试开发一个多个图表(条形图、线条图、饼形图)的视图控制器。我创建了一个表视图和自定义表视图单元格。自定义表视图单元格中有一个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 项目链接中。
发布于 2017-12-14 21:13:44
您需要为想要在TableView中使用的每种图表类型创建原型单元格。在每个原型单元中,您需要放置UIView,然后将UIView类更改为LineChartView、BarChartView等。
此外,您还需要为每个原型单元定义自己的类,例如:
class MyLineChartCell: UITableViewCell {
@IBOutlet weak var lineChartView: LineChartView!
func configure (/* ... */) {
lineChartView.noDataText = "You have no data"
// ...
}
}
。
为原型单元格使用此类:
然后,在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
}
发布于 2017-12-14 18:42:03
chartView不能是UIView类型,必须在声明它时具有正确的类型。您可以在tableView单元格中有3个不同的视图,如下所示:
@IBOutlet weak var barChart: BarChartView!
@IBOutlet weak var pieChart: PieChartView!
@IBOutlet weak var lineChart: LineChartView!
然后使用您需要的,取决于您想要的图形类型。如果您正在使用Storyboard,您还需要为每个视图选择类作为童话板中的BarChartView、LineChartView或PieChartView。
https://stackoverflow.com/questions/47819397
复制相似问题